树形问题,使用递归。
class Solution {
public String[] str={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
public List<String> letterCombinations(String digits) {
ArrayList<String> list=new ArrayList<String>();
if(digits.equals("")){
return list;
}
String value="";
findString(digits, 0, value,list);
return list;
}
public void findString(String digits,int index,String value,ArrayList<String> list){
if (index==digits.length()){
list.add(value);
return;
}
int num = digits.charAt(index)-'0';
for(int i =0;i<str[num].length();i++){
findString(digits, index+1, value+str[num].charAt(i),list);
}
}
}
知识点:
char-> int 字符 -‘0’