题目解析
Example: Input: “23” Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”]. Note: Although the above answer is in lexicographical order, your answer could be in any o
思路方法和代码演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 public class A17PhoneCombine { public List<String> letterCombinations (String digits) { List<String> result = new ArrayList <String>(); if (digits == null || digits.length() == 0 ) { return result; } Map<Character, String> dict = new HashMap <Character, String>(); dict.put('1' , "" ); dict.put('2' , "abc" ); dict.put('3' , "def" ); dict.put('4' , "ghi" ); dict.put('5' , "jkl" ); dict.put('6' , "mno" ); dict.put('7' , "pqrs" ); dict.put('8' , "tuv" ); dict.put('9' , "wxyz" ); dict.put('0' , " " ); for (char tmp : digits.toCharArray()) { String val = dict.get(tmp); if (val == null ) { return new ArrayList <String>(); } List<String> tmpArray = new ArrayList <String>(); for (int i = 0 ; i < val.length(); i++) { if (result.size() == 0 ) { result.add("" ); } for (String item : result) { tmpArray.add(item + val.charAt(i)); } } result = tmpArray; } return result; } public static void main (String[] args) { A17PhoneCombine a17PhoneCombine = new A17PhoneCombine (); System.out.println(a17PhoneCombine.letterCombinations("123" )); } }
代码文件:A17PhoneCombine.java
总结
版权声明:本文为博主原创文章,未经允许不得转载。