错好多
recursive的下次再写吧
最严重的错误就是 每次循环想要把原来list里面每个string都加一个字母的时候 注意list要重新建 不能for each str in list 然后再里面又在这同一个list上面操作
public class Solution {
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<String>();
List<String> nums = Arrays.asList("0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz");
if ( digits == null || digits.length() == 0 )
return res;
res.add("");
for ( int i = 0; i < digits.length(); i ++ ){
List<String> temp = new ArrayList<String>();
for ( String str : res ){
int num = digits.charAt(i) - '0';
int end = 3;
if ( num == 7 || num == 9 )
end = 4;
for ( int j = 0; j < end; j ++ ){
String tempstr = str + nums.get(num).charAt(j);
temp.add(tempstr);
}
}
res = temp;
}
return res;
}
}

本文介绍了一种使用递归方式生成电话键盘上数字对应字母的所有可能组合的算法。该算法通过构建临时列表来避免直接修改原始列表,确保了数据结构的正确更新。文章详细展示了如何针对每个数字遍历其对应的字母,并将这些字母添加到当前字符串中以形成新的组合。
262

被折叠的 条评论
为什么被折叠?



