利用回溯的思想去求解本题,排列组合问题,先枚举每一个位置上数字对应的字母,每一层都是遍历选择,通过标记当前层数来判断当前是否已经完成一次排列组合,然后回退到上一层,以此类推。
最后呈上代码
package main
import "fmt"
var phoneMap map[string]string = map[string]string{
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "xzy",
}
var combinations []string
func letterCombinations(digits string) []string {
if len(digits) == 0 {
return []string{}
}
combinations = []string{}
backtrack(digits, 0, "")
return combinations
}
func backtrack(digits string, index int, combination string) {
// 递归结束的条件
if index == len(digits) {
combinations = append(combinations, combination)
} else {
// 取到每一层的数字
digit := string(digits[index])
// 数字在电话按键上对应的字母
letters := phoneMap[digit]
lettersCount := len(letters)
// 遍历该数字对应的所有字母
for i := 0; i < lettersCount; i++ {
backtrack(digits, index+1, combination+string(letters[i]))
}
}
}
func main() {
digits := "23"
fmt.Println(letterCombinations(digits))
}