mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
Problem0017 add solution two
This commit is contained in:
@ -14,8 +14,10 @@ var (
|
|||||||
"wxyz", //9
|
"wxyz", //9
|
||||||
}
|
}
|
||||||
res = []string{}
|
res = []string{}
|
||||||
|
final = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 解法一 DFS
|
||||||
func letterCombinations(digits string) []string {
|
func letterCombinations(digits string) []string {
|
||||||
if digits == "" {
|
if digits == "" {
|
||||||
return []string{}
|
return []string{}
|
||||||
@ -37,3 +39,30 @@ func findCombination(digits *string, index int, s string) {
|
|||||||
}
|
}
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
@ -51,8 +51,10 @@ var (
|
|||||||
"wxyz", //9
|
"wxyz", //9
|
||||||
}
|
}
|
||||||
res = []string{}
|
res = []string{}
|
||||||
|
final = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 解法一 DFS
|
||||||
func letterCombinations(digits string) []string {
|
func letterCombinations(digits string) []string {
|
||||||
if digits == "" {
|
if digits == "" {
|
||||||
return []string{}
|
return []string{}
|
||||||
@ -75,5 +77,33 @@ func findCombination(digits *string, index int, s string) {
|
|||||||
return
|
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