|
43 | 43 | return $res;
|
44 | 44 | }
|
45 | 45 | ```
|
| 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 | + |
46 | 85 | ****
|
47 | 86 |
|
48 | 87 | ### 联系
|
|
0 commit comments