11/3
今天做了作业,比较烦
然后做的题目是17.电话号码的字母组合
这道题没有思路,用的是官方的题解,学习到了回溯的一部分内容,还有递归的使用。
具体代码如下:
class Solution {
public List<String> letterCombinations(String digits) {
var combinations = new ArrayList<String>();
if (digits.length()==0){
return combinations;
}
var phoneMap=new HashMap<Character,String>();
phoneMap.put('2',"abc");
phoneMap.put('3',"def");
phoneMap.put('4',"ghi");
phoneMap.put('5',"jkl");
phoneMap.put('6',"mno");
phoneMap.put('7',"pqrs");
phoneMap.put('8',"tuv");
phoneMap.put('9',"wxyz");
backtrack(combinations,phoneMap,digits,0,new StringBuilder());
return combinations;
}
private static void backtrack(List<String> combinations, Map<Character, String> phoneMap, String digits, int index, StringBuilder combination)
{
if (index==digits.length()){
combinations.add(combination.toString());
}else {
char digit=digits.charAt(index);
String letter = phoneMap.get(digit);
int len=letter.length();
for (int i = 0; i < len; i++) {
combination.append(letter.charAt(i));
backtrack(combinations,phoneMap,digits,index+1,combination);
combination.deleteCharAt(index);
}
}
}
}
```
这道题,难点在于构造出来的函数,首先,需要一个list集合来存储输出的结果,其次有一个hashmap集合来存放各个数字所对应的字母来选择输出,有个下标来指示目前指向到哪个输入的字符,这道题,需要消化理解才行,然后最后有一个递归。
今天的任务就是思考完成这道题!
啊啊啊啊,加油啊