Leetcode:17. 电话号码的字母组合
作者:互联网
一说起排列组合就是树形递归,到现在也见了不少这种类型的题了,还是不会,再记录下吧
思路:
利用数组索引和字符串对应,将字符串存到字符数组里,产生对应关系。之后就要想怎么处理递归,参数要求,根据输入的数字字符,转换为数字,从而从字符数组中取出对应的字符串,遍历字符串,每次加上索引对应的单个字符,如果字符串的长度==题中给的字符数组长度,说明走到了树的叶子节点,将生成的字符串加入到集合中
代码:
class Solution {
List<String> res=new ArrayList<String>();
String[] ss={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
public List<String> letterCombinations(String digits) {
if(digits==null ||digits.length()==0) return res;
findZuHe(digits,0,"");
return res;
}
private void findZuHe(String digits, int index, String s) {
if(index==digits.length()){
res.add(s);
return;
}
char c = digits.charAt(index);
String s1 = ss[c - '0'];
for (int i = 0; i <s1.length() ; i++) {
findZuHe(digits,index+1,s+s1.charAt(i));
}
}
}
标签:digits,index,String,17,字符,res,字符串,字母组合,Leetcode 来源: https://blog.csdn.net/qq_43430343/article/details/120795459