Skip to content

Commit 2b164b4

Browse files
author
吴亲强
committed
【add go for 17】
1 parent c8cb9a0 commit 2b164b4

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

0-50/17.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,45 @@
4343
return $res;
4444
}
4545
```
46+
47+
```go
48+
func letterCombinations(digits string) []string {
49+
cache := make(map[string][]string)
50+
cache["2"] = []string{"a", "b", "c"}
51+
cache["3"] = []string{"d", "e", "f"}
52+
cache["4"] = []string{"g", "h", "i"}
53+
cache["5"] = []string{"j", "k", "l"}
54+
cache["6"] = []string{"m", "n", "o"}
55+
cache["7"] = []string{"p", "q", "r", "s"}
56+
cache["8"] = []string{"t", "u", "v"}
57+
cache["9"] = []string{"w", "x", "y", "z"}
58+
59+
var helper func(cache map[string][]string, remain string) []string
60+
61+
helper = func(cache map[string][]string, remain string) []string {
62+
if len(remain) == 0 {
63+
return []string{}
64+
}
65+
66+
if res, ok := cache[remain]; ok {
67+
return res
68+
}
69+
70+
var res []string
71+
72+
first := helper(cache, remain[0:1])
73+
other := helper(cache, remain[1:])
74+
for _, item := range first {
75+
for _, otherItem := range other {
76+
res = append(res, item+otherItem)
77+
}
78+
}
79+
return res
80+
}
81+
return helper(cache, digits)
82+
}
83+
```
84+
4685
****
4786

4887
### 联系

0 commit comments

Comments
 (0)