48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
51
+ ** 方法一:遍历**
52
+
53
+ 我们先用一个数组或者哈希表存储每个数字对应的字母,然后遍历每个数字,将其对应的字母与之前的结果进行组合,得到新的结果。
54
+
55
+ 时间复杂度 $O(4^n)$。其中 $n$ 是输入数字的长度。
56
+
51
57
<!-- tabs:start -->
52
58
53
59
### ** Python3**
57
63
``` python
58
64
class Solution :
59
65
def letterCombinations (self , digits : str ) -> List[str ]:
60
- n = len (digits)
61
- if n == 0 :
66
+ if not digits:
62
67
return []
63
- chars = [' abc' , ' def' , ' ghi' , ' jkl' , ' mno' , ' pqrs' , ' tuv' , ' wxyz' ]
64
- strs = [chars[int (d) - 2 ] for d in digits]
65
- res = []
66
- for s in strs:
67
- if not res:
68
- res = list (s)
69
- else :
70
- cache = []
71
- for item in res:
72
- for letter in s:
73
- cache.append(item + letter)
74
- res = cache
75
- return res
68
+ d = [' abc' , ' def' , ' ghi' , ' jkl' , ' mno' , ' pqrs' , ' tuv' , ' wxyz' ]
69
+ ans = [' ' ]
70
+ for i in digits:
71
+ s = d[int (i) - 2 ]
72
+ ans = [a + b for a in ans for b in s]
73
+ return ans
76
74
```
77
75
78
76
### ** Java**
@@ -82,107 +80,124 @@ class Solution:
82
80
``` java
83
81
class Solution {
84
82
public List<String > letterCombinations (String digits ) {
85
- int n;
86
- if ((n = digits. length()) == 0 ) return Collections . emptyList();
87
- List<String > chars
88
- = Arrays . asList(" abc" , " def" , " ghi" , " jkl" , " mno" , " pqrs" , " tuv" , " wxyz" );
89
-
90
- List<String > strs = new ArrayList<> ();
91
- for (char c : digits. toCharArray()) {
92
- strs. add(chars. get(c - ' 0' - 2 ));
83
+ List<String > ans = new ArrayList<> ();
84
+ if (digits. length() == 0 ) {
85
+ return ans;
93
86
}
94
- List<String > res = new ArrayList<> ();
95
- for (String str : strs) {
96
- if (res. size() == 0 ) {
97
- for (char c : str. toCharArray()) {
98
- res. add(String . valueOf(c));
99
- }
100
- } else {
101
- List<String > cache = new ArrayList<> ();
102
- for (String item : res) {
103
- for (char c : str. toCharArray()) {
104
- cache. add(item + String . valueOf(c));
105
- }
87
+ ans. add(" " );
88
+ String [] d = new String [] {" abc" , " def" , " ghi" , " jkl" , " mno" , " pqrs" , " tuv" , " wxyz" };
89
+ for (char i : digits. toCharArray()) {
90
+ String s = d[i - ' 2' ];
91
+ List<String > t = new ArrayList<> ();
92
+ for (String a : ans) {
93
+ for (String b : s. split(" " )) {
94
+ t. add(a + b);
106
95
}
107
- res = cache;
108
96
}
97
+ ans = t;
109
98
}
110
- return res ;
99
+ return ans ;
111
100
}
112
101
}
113
102
```
114
103
104
+ ### ** C++**
105
+
106
+ ``` cpp
107
+ class Solution {
108
+ public:
109
+ vector<string > letterCombinations(string digits) {
110
+ if (digits.empty()) return {};
111
+ vector<string > d = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
112
+ vector<string > ans = {""};
113
+ for (auto i : digits) {
114
+ string s = d[ i - '2'] ;
115
+ vector<string > t;
116
+ for (auto& a : ans) {
117
+ for (auto& b : s) {
118
+ t.push_back(a + b);
119
+ }
120
+ }
121
+ ans = move(t);
122
+ }
123
+ return ans;
124
+ }
125
+ };
126
+ ```
127
+
115
128
### **Go**
116
129
117
130
```go
118
- var table = map [string ][]string {
119
- " 2" : {" a" , " b" , " c" },
120
- " 3" : {" d" , " e" , " f" },
121
- " 4" : {" g" , " h" , " i" },
122
- " 5" : {" j" , " k" , " l" },
123
- " 6" : {" m" , " n" , " o" },
124
- " 7" : {" p" , " q" , " r" , " s" },
125
- " 8" : {" t" , " u" , " v" },
126
- " 9" : {" w" , " x" , " y" , " z" },
127
- }
128
-
129
131
func letterCombinations(digits string) []string {
130
- if digits == " " {
131
- return make ([]string , 0 )
132
+ ans := []string{}
133
+ if len(digits) == 0 {
134
+ return ans
132
135
}
133
- var result = table[string (digits[0 ])]
134
- for i := 1 ; i < len (digits); i++ {
135
- t := table[string (digits[i])]
136
- nr := make ([]string , len (result)*len (t))
137
- for j := 0 ; j < len (result); j++ {
138
- for k := 0 ; k < len (t); k++ {
139
- nr[len (t)*j+k] = result[j] + t[k]
136
+ ans = append(ans, "")
137
+ d := []string{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
138
+ for _, i := range digits {
139
+ s := d[i-'2']
140
+ t := []string{}
141
+ for _, a := range ans {
142
+ for _, b := range s {
143
+ t = append(t, a+string(b))
140
144
}
141
145
}
142
- result = nr
146
+ ans = t
143
147
}
144
- return result
148
+ return ans
145
149
}
146
150
```
147
151
152
+ ### ** JavaScript**
153
+
154
+ ``` js
155
+ /**
156
+ * @param {string} digits
157
+ * @return {string[]}
158
+ */
159
+ var letterCombinations = function (digits ) {
160
+ if (digits .length == 0 ) {
161
+ return [];
162
+ }
163
+ let ans = [' ' ];
164
+ const d = [' abc' , ' def' , ' ghi' , ' jkl' , ' mno' , ' pqrs' , ' tuv' , ' wxyz' ];
165
+ for (const i of digits) {
166
+ const s = d[parseInt (i) - 2 ];
167
+ const t = [];
168
+ for (const a of ans) {
169
+ for (const b of s) {
170
+ t .push (a + b);
171
+ }
172
+ }
173
+ ans = t;
174
+ }
175
+ return ans;
176
+ };
177
+ ```
178
+
148
179
### ** C#**
149
180
150
181
``` cs
151
- using System .Collections .Generic ;
152
- using System .Linq ;
153
-
154
182
public class Solution {
155
- private static string [] chars = {
156
- " abc" ,
157
- " def" ,
158
- " ghi" ,
159
- " jkl" ,
160
- " mno" ,
161
- " pqrs" ,
162
- " tuv" ,
163
- " wxyz"
164
- };
165
-
166
183
public IList <string > LetterCombinations (string digits ) {
167
- var numbers = digits .Where (d => d >= '2' && d <= '9' ).Select (d => d - '2' ).ToArray ();
168
- var states = new int [numbers .Length ];
169
- var results = new List <string >();
170
- if (numbers .Length == 0 ) return results ;
171
- while (true ) {
172
- results .Add (new string (states .Select ((s , j ) => chars [numbers [j ]][s ]).ToArray ()));
173
- var i = states .Length - 1 ;
174
- ++ states [i ];
175
- while (i >= 0 && states [i ] == chars [numbers [i ]].Length )
176
- {
177
- states [i ] = 0 ;
178
- -- i ;
179
- if (i >= 0 )
180
- {
181
- ++ states [i ];
184
+ var ans = new List <string >();
185
+ if (digits .Length == 0 ) {
186
+ return ans ;
187
+ }
188
+ ans .Add (" " );
189
+ string [] d = {" abc" , " def" , " ghi" , " jkl" , " mno" , " pqrs" , " tuv" , " wxyz" };
190
+ foreach (char i in digits ) {
191
+ string s = d [i - '2' ];
192
+ var t = new List <string >();
193
+ foreach (string a in ans ) {
194
+ foreach (char b in s ) {
195
+ t .Add (a + b );
182
196
}
183
197
}
184
- if ( i < 0 ) return results ;
198
+ ans = t ;
185
199
}
200
+ return ans ;
186
201
}
187
202
}
188
203
```
0 commit comments