diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index dd820a39..12a45a61 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -120,7 +120,7 @@ for (int i = 0; i < letters.size(); i++) { **注意这里for循环,可不像是在[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)和[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)中从startIndex开始遍历的**。 -**因为本题每一个数字代表的是不同集合,也就是求不同集合之间的组合,而[77. 组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)都是是求同一个集合中的组合!** +**因为本题每一个数字代表的是不同集合,也就是求不同集合之间的组合,而[77. 组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)都是求同一个集合中的组合!** 注意:输入1 * #按键等等异常情况 @@ -356,38 +356,32 @@ class Solution: 主要在于递归中传递下一个数字 ```go +var ( + m []string + path []byte + res []string +) func letterCombinations(digits string) []string { - lenth:=len(digits) - if lenth==0 ||lenth>4{ - return nil + m = []string{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"} + path, res = make([]byte, 0), make([]string, 0) + if digits == "" { + return res } - digitsMap:= [10]string{ - "", // 0 - "", // 1 - "abc", // 2 - "def", // 3 - "ghi", // 4 - "jkl", // 5 - "mno", // 6 - "pqrs", // 7 - "tuv", // 8 - "wxyz", // 9 - } - res:=make([]string,0) - recursion("",digits,0,digitsMap,&res) - return res + dfs(digits, 0) + return res } -func recursion(tempString ,digits string, Index int,digitsMap [10]string, res *[]string) {//index表示第几个数字 - if len(tempString)==len(digits){//终止条件,字符串长度等于digits的长度 - *res=append(*res,tempString) +func dfs(digits string, start int) { + if len(path) == len(digits) { //终止条件,字符串长度等于digits的长度 + tmp := string(path) + res = append(res, tmp) return } - tmpK:=digits[Index]-'0' // 将index指向的数字转为int(确定下一个数字) - letter:=digitsMap[tmpK]// 取数字对应的字符集 - for i:=0;i