一:题目
二:上码
class Solution {
private List<String> ans = new ArrayList<>();
private List<Character> path = new ArrayList<>();
public String getStr(char ch) {
if (ch == '2') return "abc";
else if (ch == '3') return "def";
else if (ch == '4') return "ghi";
else if (ch == '5') return "jkl";
else if (ch == '6') return "mno";
else if (ch == '7') return "pqrs";
else if (ch == '8') return "tuv";
else return "wxyz";
}
public void getAns(String digits,int index) {
if (path.size() == digits.length()) {
StringBuilder sb = new StringBuilder();
for (char ch:path) {
sb.append(ch);
}
ans.add(sb.toString());
return;
}
char ch = digits.charAt(index);
String str = getStr(ch);
for (int i = 0; i < str.length(); i++) {
path.add(str.charAt(i));
getAns(digits,index+1);
path.remove(path.size()-1);
}
}
public List<String> letterCombinations(String digits) {
if (digits.equals("")) return ans;
getAns(digits,0);
return ans;
}
}