对于犯的那两个错误,请正视!!,尤其是第一个,否则一上来就挂了。。
public class Solution {
public List<String> letterCombinations(String digits) {
List<String> results = new LinkedList<>();
if (digits == null || digits.length() == 0) {
return results;
}
Map<Character, char[]> digitsMap = new HashMap<Character, char[]>();
digitsMap.put('0', new char[] {});
digitsMap.put('1', new char[] {});
digitsMap.put('2', new char[] {'a','b','c'});
digitsMap.put('3', new char[] {'d','e','f'});
digitsMap.put('4', new char[] {'g','h','i'});
digitsMap.put('5', new char[] {'j','k','l'});
digitsMap.put('6', new char[] {'m','n','o'});
digitsMap.put('7', new char[] {'p','q','r','s'});
digitsMap.put('8', new char[] {'t','u','v'});
digitsMap.put('9', new char[] {'w','x','y','z'});
StringBuilder sb = new StringBuilder();
letterCombinationsHelper(digits, results, sb, 0, digitsMap);
return results;
}
private void letterCombinationsHelper(String digits, List<String> results, StringBuilder sb, int pos, Map<Character, char[]> digitsMap) {
//if (sb.size() == digits.length()) {
if (sb.length() == digits.length()) {
results.add(new String(sb.toString()));
return;
}
for (int i = pos; i < digits.length(); i++) {
char[] ca = digitsMap.get(digits.charAt(i));
for(char ta: ca) {
sb.append(ta);
letterCombinationsHelper(digits, results, sb, i + 1, digitsMap);
//sb.remove(sb.length() - 1);
sb.deleteCharAt(sb.length() - 1);
}
}
}
}