刷了两天题,好像以前不会的东西现在可以做出来了。
把搜索的做一做之后会写一个总结,现在是把每道题都单独写出来了。
而且我发现我没有写注释。。。。。
Example:
Input: “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
思路:
图转自b站花花酱
class Solution {
public List<String> letterCombinations(String digits) {
String[] dic = new String[10];
dic[0] = "";
dic[1] = "";
dic[2] = "abc";
dic[3] = "def";
dic[4] = "ghi";
dic[5] = "jkl";
dic[6] = "mno";
dic[7] = "pqrs";
dic[8] = "tuv";
dic[9] = "wxyz";
if(digits == null || digits.length()==0)
return new LinkedList<String>();
List<String> results = new LinkedList<String>();
char[] list = new char[digits.length()];
helper(digits, results, list, dic, 0);
return results;
}
public void helper(String digits, List<String> results, char[] list, String[] dic, int n){
if(n == digits.length()){
results.add(new String(list));
return;
}
String str = dic[digits.charAt(n) - '0'];
for(int i = 0; i < str.length(); i++){
list[n] = str.charAt(i);
helper(digits, results, list, dic, n + 1);
}
}
}
总结:
- 说实话String[] dic这里还在想是写个list还是啥的。
- String str = dic[digits.charAt(n) - ‘0’];这个地方还挺有意思的,是这道题的精髓。