思路
设置一个char型的二维数组,每次从号码对应的子母中选出一个,拼接在一起即可
解题方法
注意:有的数字对应三个字母,有的对应四个字母
Code
class Solution {public char arr[][]={{'a','b','c',' '},{'d','e','f',' '},{'g','h','i',' '},{'j','k','l',' '},{'m','n','o',' '},{'p','q','r','s'},{'t','u','v',' '},{'w','x','y','z'}};public List<String> ans=new ArrayList<String>();public int len;public List<String> letterCombinations(String digits) {if(digits.isEmpty())return ans;len=digits.length();dfs(0,digits,"");return ans;}public void dfs(int i,String digits,String t){if(i==len){ans.add(t);return;}int row=digits.charAt(i)-'2';int col;if(arr[row][3]==' ')col=3;else col=4;for(int j=0;j<col;j++){dfs(i+1,digits,t+arr[row][j]);}}
}