Skip to content

Commit b04e941

Browse files
committed
feat: add solutions to lc problem: No.0017
No.0017.Letter Combinations of a Phone Number
1 parent 819f209 commit b04e941

File tree

9 files changed

+294
-271
lines changed

9 files changed

+294
-271
lines changed

solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md

Lines changed: 104 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:遍历**
52+
53+
我们先用一个数组或者哈希表存储每个数字对应的字母,然后遍历每个数字,将其对应的字母与之前的结果进行组合,得到新的结果。
54+
55+
时间复杂度 $O(4^n)$。其中 $n$ 是输入数字的长度。
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**
@@ -57,22 +63,14 @@
5763
```python
5864
class Solution:
5965
def letterCombinations(self, digits: str) -> List[str]:
60-
n = len(digits)
61-
if n == 0:
66+
if not digits:
6267
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
7674
```
7775

7876
### **Java**
@@ -82,107 +80,124 @@ class Solution:
8280
```java
8381
class Solution {
8482
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;
9386
}
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);
10695
}
107-
res = cache;
10896
}
97+
ans = t;
10998
}
110-
return res;
99+
return ans;
111100
}
112101
}
113102
```
114103

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+
115128
### **Go**
116129
117130
```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-
129131
func letterCombinations(digits string) []string {
130-
if digits == "" {
131-
return make([]string, 0)
132+
ans := []string{}
133+
if len(digits) == 0 {
134+
return ans
132135
}
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))
140144
}
141145
}
142-
result = nr
146+
ans = t
143147
}
144-
return result
148+
return ans
145149
}
146150
```
147151

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+
148179
### **C#**
149180

150181
```cs
151-
using System.Collections.Generic;
152-
using System.Linq;
153-
154182
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-
166183
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);
182196
}
183197
}
184-
if (i < 0) return results;
198+
ans = t;
185199
}
200+
return ans;
186201
}
187202
}
188203
```

0 commit comments

Comments
 (0)