-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathletterCombinations.go
78 lines (71 loc) · 1.41 KB
/
letterCombinations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package leetcode
import "sort"
var pMap = map[string]string{
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
func letterCombinations(digits string) []string {
if digits == "" {
return nil
}
return backtrace("", digits)
}
func backtrace(letters, nextDigit string) (res []string) {
if nextDigit == "" {
res = append(res, letters)
return
}
d := nextDigit[:1]
for _, v := range pMap[d] {
res = append(res, backtrace(letters+string(v), nextDigit[1:])...)
}
return
}
// rune.
func letterCombinations1(digits string) []string {
if digits == "" {
return nil
}
res := [][]rune{[]rune("")}
for _, d := range digits {
nextRes := [][]rune{}
for _, letter := range pMap[string(d)] {
for _, v := range res {
// tmp := make([]rune, len(v))
// copy(tmp, v) // 拷贝rune,避免重复修改 v
nextRes = append(nextRes, append([]rune(string(v)), letter))
}
}
res = nextRes
}
out := make([]string, len(res))
for k := range res {
out[k] = string(res[k])
}
sort.Strings(out)
return out
}
// string.
func letterCombinations2(digits string) []string {
if digits == "" {
return nil
}
res := []string{""}
for _, d := range digits {
nextRes := []string{}
for _, letter := range pMap[string(d)] {
for _, v := range res {
nextRes = append(nextRes, v+string(letter))
}
}
res = nextRes
}
sort.Strings(res)
return res
}