Skip to content

Commit 6d261ee

Browse files
committed
feat: add solutions to leetcode problem: No.0500
1 parent e6ba4df commit 6d261ee

File tree

4 files changed

+113
-46
lines changed

4 files changed

+113
-46
lines changed

solution/0500-0599/0500.Keyboard Row/README.md

+49-2
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,63 @@
4040

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

43-
```python
43+
用三个集合存储键盘每一行字母。
44+
45+
遍历单词列表 `words`,判断每个单词 `word` 转 set 后是否被以上三个集合其中之一包含,若是,将单词添加到结果数组。
4446

47+
```python
48+
class Solution:
49+
def findWords(self, words: List[str]) -> List[str]:
50+
s1 = set('qwertyuiop')
51+
s2 = set('asdfghjkl')
52+
s3 = set('zxcvbnm')
53+
res = []
54+
for word in words:
55+
t = set(word.lower())
56+
if t <= s1 or t <= s2 or t <= s3:
57+
# 利用 python set 比较
58+
res.append(word)
59+
return res
4560
```
4661

4762
### **Java**
4863

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

51-
```java
66+
用三个字符串存储键盘每一行字母。
5267

68+
遍历单词列表 `words`,对于每个单词 `word`
69+
70+
1. 分别设置三个计数器,存储当前单词在对应键盘字符串的字母个数;
71+
2. 遍历 `word` 中的每个字母,如果在对应的键盘字符串中,则对应的计数器加 1;
72+
3. 单词遍历结束后,判断是否存在一个计数器值与 `word` 长度相同。如果有,说明该单词所有字母都在同一个键盘字符串中,将单词添加到结果数组。
73+
74+
```java
75+
class Solution {
76+
public String[] findWords(String[] words) {
77+
String s1 = "qwertyuiopQWERTYUIOP";
78+
String s2 = "asdfghjklASDFGHJKL";
79+
String s3 = "zxcvbnmZXCVBNM";
80+
List<String> res = new ArrayList<>();
81+
for (String word : words) {
82+
int n1 = 0, n2 = 0, n3 = 0;
83+
int n = word.length();
84+
for (int i = 0; i < n; ++i) {
85+
if (s1.contains(String.valueOf(word.charAt(i)))) {
86+
++n1;
87+
} else if (s2.contains(String.valueOf(word.charAt(i)))) {
88+
++n2;
89+
} else {
90+
++n3;
91+
}
92+
}
93+
if (n1 == n || n2 == n || n3 == n) {
94+
res.add(word);
95+
}
96+
}
97+
return res.toArray(new String[0]);
98+
}
99+
}
53100
```
54101

55102
### **...**

solution/0500-0599/0500.Keyboard Row/README_EN.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,47 @@
3838
### **Python3**
3939

4040
```python
41-
41+
class Solution:
42+
def findWords(self, words: List[str]) -> List[str]:
43+
s1 = set('qwertyuiop')
44+
s2 = set('asdfghjkl')
45+
s3 = set('zxcvbnm')
46+
res = []
47+
for word in words:
48+
t = set(word.lower())
49+
if t <= s1 or t <= s2 or t <= s3:
50+
res.append(word)
51+
return res
4252
```
4353

4454
### **Java**
4555

4656
```java
47-
57+
class Solution {
58+
public String[] findWords(String[] words) {
59+
String s1 = "qwertyuiopQWERTYUIOP";
60+
String s2 = "asdfghjklASDFGHJKL";
61+
String s3 = "zxcvbnmZXCVBNM";
62+
List<String> res = new ArrayList<>();
63+
for (String word : words) {
64+
int n1 = 0, n2 = 0, n3 = 0;
65+
int n = word.length();
66+
for (int i = 0; i < n; ++i) {
67+
if (s1.contains(String.valueOf(word.charAt(i)))) {
68+
++n1;
69+
} else if (s2.contains(String.valueOf(word.charAt(i)))) {
70+
++n2;
71+
} else {
72+
++n3;
73+
}
74+
}
75+
if (n1 == n || n2 == n || n3 == n) {
76+
res.add(word);
77+
}
78+
}
79+
return res.toArray(new String[0]);
80+
}
81+
}
4882
```
4983

5084
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
class Solution {
2-
32
public String[] findWords(String[] words) {
4-
if (words == null) {
5-
return null;
6-
}
7-
ArrayList<String> list = new ArrayList<>();
8-
String[] keyboards = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
9-
for (int i = 0; i < words.length; i++) {
10-
String word = words[i].toLowerCase();
11-
for (int j = 0; j < keyboards.length; j++) {
12-
// 先用word首字符确定属于哪一行
13-
if (keyboards[j].indexOf(word.charAt(0)) > -1) {
14-
// 判断word字符串所有字符是否都属于同一行
15-
boolean match = match(keyboards[j], word, list);
16-
if (match) {
17-
list.add(words[i]);
18-
}
19-
break;
3+
String s1 = "qwertyuiopQWERTYUIOP";
4+
String s2 = "asdfghjklASDFGHJKL";
5+
String s3 = "zxcvbnmZXCVBNM";
6+
List<String> res = new ArrayList<>();
7+
for (String word : words) {
8+
int n1 = 0, n2 = 0, n3 = 0;
9+
int n = word.length();
10+
for (int i = 0; i < n; ++i) {
11+
if (s1.contains(String.valueOf(word.charAt(i)))) {
12+
++n1;
13+
} else if (s2.contains(String.valueOf(word.charAt(i)))) {
14+
++n2;
15+
} else {
16+
++n3;
2017
}
2118
}
22-
}
23-
return list.toArray(new String[list.size()]);
24-
}
25-
26-
private boolean match(String keyboard, String word, ArrayList<String> list) {
27-
for (int i = 1; i < word.length(); i++) {
28-
if (keyboard.indexOf(word.charAt(i)) < 0) {
29-
return false;
19+
if (n1 == n || n2 == n || n3 == n) {
20+
res.add(word);
3021
}
3122
}
32-
return true;
23+
return res.toArray(new String[0]);
3324
}
34-
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
class Solution:
2-
def findWords(self, words):
3-
"""
4-
:type words: List[str]
5-
:rtype: List[str]
6-
"""
7-
8-
Rows={"q":0, "w":0, "e":0, "r":0, "t":0, "y":0, "u":0, "i":0, "o":0, "p":0, "a":1, "s":1, "d":1, "f":1, "g":1, "h":1, "j":1, "k":1, "l":1, "z":2, "x":2, "c":2, "v":2, "b":2, "n":2, "m":2}
9-
10-
Final = []
11-
for each in words :
12-
13-
if all([Rows[each[0].lower()]==Rows[ch] for ch in each.lower()]) :
14-
Final.append(each)
15-
16-
return Final
2+
def findWords(self, words: List[str]) -> List[str]:
3+
s1 = set('qwertyuiop')
4+
s2 = set('asdfghjkl')
5+
s3 = set('zxcvbnm')
6+
res = []
7+
for word in words:
8+
t = set(word.lower())
9+
if t <= s1 or t <= s2 or t <= s3:
10+
res.append(word)
11+
return res

0 commit comments

Comments
 (0)