题目:
思路:设置两个List,一个存储当前层,一个存储最终层
public class Solution {
public List<String> letterCombinations(String digits) {
List<String> listRet=new ArrayList<String>();
if(digits==""){
return listRet;
}
Map<Character,String> map=new HashMap<Character,String>();
map.put('0',"0");
map.put('1',"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"); for(int i=0;i<digits.length();i++){
List<String> listTem=new ArrayList<String>();
if(i==0){
char[] initials=map.get(digits.charAt(0)).toCharArray();
for(char initial:initials){
listRet.add(String.valueOf(initial));
}
continue;
}
String s1=map.get(digits.charAt(i));
char[] letters=s1.toCharArray();//当前数字对应字符串
for(char letter:letters){
for(String pre:listRet){
listTem.add(pre+String.valueOf(letter));//前缀+当前层字符
}
}
listRet=listTem;
}
return listRet;
}
}