给出一串数字串,每一个数字对应九字格的手机键盘,
问这么一串数字串所有的字母组合
<span style="font-size:18px;">public class Solution {
private static List<String> number = new ArrayList<>();
{
number.add("");number.add("");number.add("abc");
number.add("def");number.add("ghi");number.add("jkl");
number.add("mno");number.add("pqrs");number.add("tuv");
number.add("wxyz");
}
static char[] sb ;
static String no;
static List<String> l = new ArrayList<>();
public List<String> letterCombinations(String digits) {
sb = new char[100];
l.clear();
this.no = digits;
dfs(0, 0);
return l;
}
public void dfs(int cur, int idx){
if(cur == no.length()){
l.add(new String(sb, 0, idx));
return;
}
int i = no.charAt(cur) - '0';
if(i == 0 || i == 1){
dfs(cur + 1, idx);
}
else for(char c : number.get(i).toCharArray()){
sb[idx] = c;
dfs(cur + 1, idx + 1);
}
}
}</span>