17. Letter Combinations of a Phone Number

---恢复内容开始---

不一定都要用StringBuilder 可能用String更方便
https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/8118/Easy-understand-Java-Solution

 

 

 

 1 class Solution {
 2     HashMap<Character, String> map = new HashMap<>();
 3     public List<String> letterCombinations(String digits) {
 4         List<StringBuilder> list = new ArrayList<>();
 5         
 6         map.put('2', "abc");
 7         map.put('3', "def");
 8         map.put('4', "ghi");
 9         map.put('5', "jkl");
10         map.put('6', "mno");
11         map.put('7', "pqrs");
12         map.put('8', "tuv");
13         map.put('9', "wxyz");
14         char[] digs = digits.toCharArray();
15         for(char dig : digs) {
16             String eachNum = map.get(dig);
17             list = contain(eachNum, list);
18             
19         }
20         List<String> res = new ArrayList<>();
21         for(int i = 0; i< list.size(); i++) {
22             res.add(list.get(i).toString());
23         }
24         return res;    
25     }
26     
27     public List<StringBuilder> contain(String eachNum, List<StringBuilder> list) {
28         List<StringBuilder> temp = new ArrayList<>();
29         for(int i = 0; i < eachNum.length(); i++) {
30             if(list.size() == 0) {
31                 temp.add(new StringBuilder("" + eachNum.charAt(i)));
32             
33             }else {
34                 for(int j = 0; j < list.size(); j++) {
35                     temp.add(new StringBuilder(list.get(j)).append(eachNum.charAt(i)));
36                 }
37             }    
38             
39         }
40         return temp;
41     }
42 }

 

---恢复内容结束---

不一定都要用StringBuilder 可能用String更方便
https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/8118/Easy-understand-Java-Solution

 

 

 

 1 class Solution {
 2     HashMap<Character, String> map = new HashMap<>();
 3     public List<String> letterCombinations(String digits) {
 4         List<StringBuilder> list = new ArrayList<>();
 5         
 6         map.put('2', "abc");
 7         map.put('3', "def");
 8         map.put('4', "ghi");
 9         map.put('5', "jkl");
10         map.put('6', "mno");
11         map.put('7', "pqrs");
12         map.put('8', "tuv");
13         map.put('9', "wxyz");
14         char[] digs = digits.toCharArray();
15         for(char dig : digs) {
16             String eachNum = map.get(dig);
17             list = contain(eachNum, list);
18             
19         }
20         List<String> res = new ArrayList<>();
21         for(int i = 0; i< list.size(); i++) {
22             res.add(list.get(i).toString());
23         }
24         return res;    
25     }
26     
27     public List<StringBuilder> contain(String eachNum, List<StringBuilder> list) {
28         List<StringBuilder> temp = new ArrayList<>();
29         for(int i = 0; i < eachNum.length(); i++) {
30             if(list.size() == 0) {
31                 temp.add(new StringBuilder("" + eachNum.charAt(i)));
32             
33             }else {
34                 for(int j = 0; j < list.size(); j++) {
35                     temp.add(new StringBuilder(list.get(j)).append(eachNum.charAt(i)));
36                 }
37             }    
38             
39         }
40         return temp;
41     }
42 }

 

posted @ 2018-09-06 11:32  jasoncool1  阅读(162)  评论(0编辑  收藏  举报