码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode17电话号码字母组合

时间:2020-10-09 21:29:26      阅读:33      评论:0      收藏:0      [点我收藏+]

标签:ons   generated   lis   port   method   arraylist   shm   pre   import   

package leetcode17;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PhoneNumberDemo01 {
}
class Solution {
 public List<String> letterCombinations(String digits) {
  Map<Integer, List<Character>> map = new HashMap<Integer, List<Character>>();
  map.put(2, Arrays.asList(‘a‘, ‘b‘, ‘c‘));
  map.put(3, Arrays.asList(‘d‘, ‘e‘, ‘f‘));
  map.put(4, Arrays.asList(‘g‘, ‘h‘, ‘i‘));
  map.put(5, Arrays.asList(‘j‘, ‘k‘, ‘l‘));
  map.put(6, Arrays.asList(‘m‘, ‘n‘, ‘o‘));
  map.put(7, Arrays.asList(‘p‘, ‘q‘, ‘r‘, ‘s‘));
  map.put(8, Arrays.asList(‘t‘, ‘u‘, ‘v‘));
  map.put(9, Arrays.asList(‘w‘, ‘x‘, ‘y‘, ‘z‘));
  int len = digits.length();
  
  List<String> list = new ArrayList<String>();
  List<Character> path = new ArrayList<Character>();
  if(len==0) {
   return list;
  }
  dfs("",list,len,0,map,path);
 }
 private void dfs(String res,List<String> list, int len,int depth, Map<Integer, List<Character>> map,List<Character> path ) {
  // TODO Auto-generated method stub
  if(len == depth) {
//   list.add(pa);
   list.add(res);
  }
  List<Character> list2 = map.get(depth);
  for(int i=0;i<list2.size();i++) {
   dfs(res+, list, len, depth, map, path);
  }
 }
}

  

leetcode17电话号码字母组合

标签:ons   generated   lis   port   method   arraylist   shm   pre   import   

原文地址:https://www.cnblogs.com/lt123/p/13787124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!