December 17, 2019
https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/
使用语言:Go
func letterCombinations(digits string) []string {
m := make(map[string][]string)
m["1"] = []string{}
m["2"] = []string{"a", "b", "c"}
m["3"] = []string{"d", "e", "f"}
m["4"] = []string{"g", "h", "i"}
m["5"] = []string{"j", "k", "l"}
m["6"] = []string{"m", "n", "o"}
m["7"] = []string{"p", "q", "r", "s"}
m["8"] = []string{"t", "u", "v"}
m["9"] = []string{"w", "x", "y", "z"}
return _letterCombinations(digits, m)
}
func _letterCombinations(digits string, m map[string][]string) (res []string) {
if len(digits) == 0 {
return
}
if len(digits) == 1 {
return m[string(digits[0])]
}
subRes := _letaterCombinations(digits[1:], m)
for _, item := range m[string(digits[0])] {
for _, r := range subRes {
res = append(res, item + r)
}
}
return
}
题目很简单,构造一个 map 存储数字到字符串的映射,然后递归即可。
痕迹
没有过去,就没法认定现在的自己