class Solution {
String[] letters = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
int n = digits.length();
List<String> ans = new ArrayList<>();
if(n == 0) return ans;
letterCombinations(digits, 0, new StringBuilder(), ans);
return ans;
}
public void letterCombinations(String digits, int i, StringBuilder combination, List<String> ans){
if(i == digits.length()){
ans.add(combination.toString());
return;
}
int num = digits.charAt(i) - ‘0‘;
for(int j = 0; j < letters[num].length(); j++){
combination.append(letters[num].charAt(j));
letterCombinations(digits, i+1, combination, ans);
combination.deleteCharAt(combination.length()-1);
}
}
}
原文:https://www.cnblogs.com/xiafrog/p/14347907.html