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
| class Solution { public List<String> letterCombinations(String digits) { List<String> list = new ArrayList<String>(); if (digits.length()==0) return list; Map<Character,String> map = new HashMap<>(); map.put('0',""); map.put('1'," "); map.put('2',"abc"); map.put('3',"def"); map.put('4',"ghi"); map.put('5',"jkl"); map.put('6',"mno"); map.put('7',"pqrs"); map.put('8',"tuv"); map.put('9',"wxyz"); select(digits,map,new StringBuffer(),0,list); return list; } public void select(String digits,Map<Character,String> map,StringBuffer stringBuffer,int index,List<String> list){ if (index==digits.length()){ list.add(stringBuffer.toString()); }else{ char c = digits.charAt(index); String s = map.get(c); int len = s.length(); for (int i = 0;i<len;i++){ stringBuffer.append(s.charAt(i)); select(digits,map,stringBuffer,index+1,list); stringBuffer.deleteCharAt(index); } } } }
|