Leetcode 17. 电话号码的字母组合
思路 回溯法
func letterCombinations(digits string) []string { if len(digits) == 0 || len(digits) > 4 { return nil } strMap := [10]string{ "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz", } res := make([]string, 0) var backTracking func(temp string, digits string, strMap [10]string, index int) backTracking = func(temp string, digits string, strMap [10]string, index int) { if len(temp) == len(digits) { //终止条件,当temp的长度和digits的长度相同时,收集结果 res = append(res, temp) return } byteIndex := digits[index] - '0' //将此字符类型的数字转化为int类型 byteStr := strMap[byteIndex] //获取此数字对应的字符集 for i := 0; i < len(byteStr); i++ { temp = temp + string(byteStr[i]) backTracking(temp, digits, strMap, index+1) temp = temp[:len(temp)-1] } } backTracking("", digits, strMap, 0) return res }