Problem0017 add solution two

This commit is contained in:
YDZ
2020-08-25 00:26:12 +08:00
parent d7ed4c32cf
commit db8c8979fd
2 changed files with 61 additions and 2 deletions

View File

@ -13,9 +13,11 @@ var (
"tuv", //8
"wxyz", //9
}
res = []string{}
res = []string{}
final = 0
)
// 解法一 DFS
func letterCombinations(digits string) []string {
if digits == "" {
return []string{}
@ -37,3 +39,30 @@ func findCombination(digits *string, index int, s string) {
}
return
}
// 解法二 非递归
func letterCombinations_(digits string) []string {
if digits == "" {
return []string{}
}
index := digits[0] - '0'
letter := letterMap[index]
tmp := []string{}
for i := 0; i < len(letter); i++ {
if len(res) == 0 {
res = append(res, "")
}
for j := 0; j < len(res); j++ {
tmp = append(tmp, res[j]+string(letter[i]))
}
}
res = tmp
final++
letterCombinations(digits[1:])
final--
if final == 0 {
tmp = res
res = []string{}
}
return tmp
}