Skip to content

Commit 5b28619

Browse files
committed
feat: update leetcode solutions: No.0017. Letter Combinations of a Phone Number
1 parent b85258f commit 5b28619

File tree

6 files changed

+135
-26
lines changed

6 files changed

+135
-26
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
### 字符串
7070

7171
- [无重复字符的最长子串](/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README.md)
72+
- [最长公共前缀](/solution/0000-0099/0014.Longest%20Common%20Prefix/README.md)
7273
- [反转字符串中的元音字母](/solution/0300-0399/0345.Reverse%20Vowels%20of%20a%20String/README.md)
7374
- [字符串转换整数 (atoi)](/solution/0000-0099/0008.String%20to%20Integer%20%28atoi%29/README.md)
7475
- [赎金信](/solution/0300-0399/0383.Ransom%20Note/README.md)

Diff for: README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
6868
### Strings
6969

7070
- [Longest Substring Without Repeating Characters](/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README_EN.md)
71+
- [Longest Common Prefix](/solution/0000-0099/0014.Longest%20Common%20Prefix/README_EN.md)
7172
- [Reverse Vowels of a String](/solution/0300-0399/0345.Reverse%20Vowels%20of%20a%20String/README_EN.md)
7273
- [String to Integer (atoi)](/solution/0000-0099/0008.String%20to%20Integer%20%28atoi%29/README_EN.md)
7374
- [Ransom Note](/solution/0300-0399/0383.Ransom%20Note/README_EN.md)

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

+47-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,60 @@
3131
<!-- 这里可写当前语言的特殊实现逻辑 -->
3232

3333
```python
34-
34+
class Solution:
35+
def letterCombinations(self, digits: str) -> List[str]:
36+
n = len(digits)
37+
if n == 0:
38+
return []
39+
chars = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
40+
strs = [chars[int(d) - 2] for d in digits]
41+
res = []
42+
for s in strs:
43+
if not res:
44+
res = list(s)
45+
else:
46+
cache = []
47+
for item in res:
48+
for letter in s:
49+
cache.append(item + letter)
50+
res = cache
51+
return res
3552
```
3653

3754
### **Java**
3855

3956
<!-- 这里可写当前语言的特殊实现逻辑 -->
4057

4158
```java
42-
59+
class Solution {
60+
public List<String> letterCombinations(String digits) {
61+
int n;
62+
if ((n = digits.length()) == 0) return Collections.emptyList();
63+
List<String> chars = Arrays.asList("abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz");
64+
65+
List<String> strs = new ArrayList<>();
66+
for (char c : digits.toCharArray()) {
67+
strs.add(chars.get(c - '0' - 2));
68+
}
69+
List<String> res = new ArrayList<>();
70+
for (String str : strs) {
71+
if (res.size() == 0) {
72+
for (char c : str.toCharArray()) {
73+
res.add(String.valueOf(c));
74+
}
75+
} else {
76+
List<String> cache = new ArrayList<>();
77+
for (String item : res) {
78+
for (char c : str.toCharArray()) {
79+
cache.add(item + String.valueOf(c));
80+
}
81+
}
82+
res = cache;
83+
}
84+
}
85+
return res;
86+
}
87+
}
4388
```
4489

4590
### **...**

Diff for: solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md

+47-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,58 @@
3131
### **Python3**
3232

3333
```python
34-
34+
class Solution:
35+
def letterCombinations(self, digits: str) -> List[str]:
36+
n = len(digits)
37+
if n == 0:
38+
return []
39+
chars = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
40+
strs = [chars[int(d) - 2] for d in digits]
41+
res = []
42+
for s in strs:
43+
if not res:
44+
res = list(s)
45+
else:
46+
cache = []
47+
for item in res:
48+
for letter in s:
49+
cache.append(item + letter)
50+
res = cache
51+
return res
3552
```
3653

3754
### **Java**
3855

3956
```java
40-
57+
class Solution {
58+
public List<String> letterCombinations(String digits) {
59+
int n;
60+
if ((n = digits.length()) == 0) return Collections.emptyList();
61+
List<String> chars = Arrays.asList("abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz");
62+
63+
List<String> strs = new ArrayList<>();
64+
for (char c : digits.toCharArray()) {
65+
strs.add(chars.get(c - '0' - 2));
66+
}
67+
List<String> res = new ArrayList<>();
68+
for (String str : strs) {
69+
if (res.size() == 0) {
70+
for (char c : str.toCharArray()) {
71+
res.add(String.valueOf(c));
72+
}
73+
} else {
74+
List<String> cache = new ArrayList<>();
75+
for (String item : res) {
76+
for (char c : str.toCharArray()) {
77+
cache.add(item + String.valueOf(c));
78+
}
79+
}
80+
res = cache;
81+
}
82+
}
83+
return res;
84+
}
85+
}
4186
```
4287

4388
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
class Solution {
22
public List<String> letterCombinations(String digits) {
3-
char[] cs = digits.toCharArray();
4-
List<String> result = new ArrayList<>();
5-
for (char a : cs) {
6-
char[] charArray;
7-
switch (a) {
8-
case '2': charArray = new char[]{'a','b','c'}; break;
9-
case '3': charArray = new char[]{'d','e','f'}; break;
10-
case '4': charArray = new char[]{'g','h','i'}; break;
11-
case '5': charArray = new char[]{'j','k','l'}; break;
12-
case '6': charArray = new char[]{'m','n','o'}; break;
13-
case '7': charArray = new char[]{'p','q','r','s'}; break;
14-
case '8': charArray = new char[]{'t','u','v'}; break;
15-
case '9': charArray = new char[]{'w','x','y','z'}; break;
16-
default: return null;
17-
}
18-
if (result.size() == 0) {
19-
for (char aCharArray : charArray) result.add(String.valueOf(aCharArray));
3+
int n;
4+
if ((n = digits.length()) == 0) return Collections.emptyList();
5+
List<String> chars = Arrays.asList("abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz");
6+
7+
List<String> strs = new ArrayList<>();
8+
for (char c : digits.toCharArray()) {
9+
strs.add(chars.get(c - '0' - 2));
10+
}
11+
List<String> res = new ArrayList<>();
12+
for (String str : strs) {
13+
if (res.size() == 0) {
14+
for (char c : str.toCharArray()) {
15+
res.add(String.valueOf(c));
16+
}
2017
} else {
2118
List<String> cache = new ArrayList<>();
22-
for (String string : result) {
23-
for (char aCharArray : charArray) cache.add(string + aCharArray);
19+
for (String item : res) {
20+
for (char c : str.toCharArray()) {
21+
cache.add(item + String.valueOf(c));
22+
}
2423
}
25-
result = cache;
24+
res = cache;
2625
}
2726
}
28-
return result;
27+
return res;
2928
}
30-
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def letterCombinations(self, digits: str) -> List[str]:
3+
n = len(digits)
4+
if n == 0:
5+
return []
6+
chars = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
7+
strs = [chars[int(d) - 2] for d in digits]
8+
res = []
9+
for s in strs:
10+
if not res:
11+
res = list(s)
12+
else:
13+
cache = []
14+
for item in res:
15+
for letter in s:
16+
cache.append(item + letter)
17+
res = cache
18+
return res

0 commit comments

Comments
 (0)