mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
Problem0017 add solution two
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user