File tree 5 files changed +147
-16
lines changed
solution/0500-0599/0500.Keyboard Row
5 files changed +147
-16
lines changed Original file line number Diff line number Diff line change 51
51
<li><code>words[i]</code> 由英文字母(小写和大写字母)组成</li>
52
52
</ul >
53
53
54
-
55
54
## 解法
56
55
57
56
<!-- 这里可写通用的实现逻辑 -->
@@ -121,6 +120,63 @@ class Solution {
121
120
}
122
121
```
123
122
123
+ ``` java
124
+ class Solution {
125
+ public String [] findWords (String [] words ) {
126
+ String s = " 12210111011122000010020202" ;
127
+ List<String > res = new ArrayList<> ();
128
+ for (String word : words) {
129
+ Set<Character > t = new HashSet<> ();
130
+ for (char c : word. toLowerCase(). toCharArray()) {
131
+ t. add(s. charAt(c - ' a' ));
132
+ }
133
+ if (t. size() == 1 ) {
134
+ res. add(word);
135
+ }
136
+ }
137
+ return res. toArray(new String [0 ]);
138
+ }
139
+ }
140
+ ```
141
+
142
+ ### ** C++**
143
+
144
+ ``` cpp
145
+ class Solution {
146
+ public:
147
+ vector<string > findWords(vector<string >& words) {
148
+ string s = "12210111011122000010020202";
149
+ vector<string > ans;
150
+ for (auto& word : words)
151
+ {
152
+ unordered_set<char > t;
153
+ for (char c : word) t.insert(s[ tolower(c) - 'a'] );
154
+ if (t.size() == 1) ans.push_back(word);
155
+ }
156
+ return ans;
157
+ }
158
+ };
159
+ ```
160
+
161
+ ### **Go**
162
+
163
+ ```go
164
+ func findWords(words []string) []string {
165
+ s := "12210111011122000010020202"
166
+ var ans []string
167
+ for _, word := range words {
168
+ t := make(map[byte]bool)
169
+ for _, c := range word {
170
+ t[s[unicode.ToLower(c)-'a']] = true
171
+ }
172
+ if len(t) == 1 {
173
+ ans = append(ans, word)
174
+ }
175
+ }
176
+ return ans
177
+ }
178
+ ```
179
+
124
180
### ** ...**
125
181
126
182
```
Original file line number Diff line number Diff line change 45
45
<li><code>words[i]</code> consists of English letters (both lowercase and uppercase). </li>
46
46
</ul >
47
47
48
-
49
48
## Solutions
50
49
51
50
<!-- tabs:start -->
@@ -96,6 +95,63 @@ class Solution {
96
95
}
97
96
```
98
97
98
+ ``` java
99
+ class Solution {
100
+ public String [] findWords (String [] words ) {
101
+ String s = " 12210111011122000010020202" ;
102
+ List<String > res = new ArrayList<> ();
103
+ for (String word : words) {
104
+ Set<Character > t = new HashSet<> ();
105
+ for (char c : word. toLowerCase(). toCharArray()) {
106
+ t. add(s. charAt(c - ' a' ));
107
+ }
108
+ if (t. size() == 1 ) {
109
+ res. add(word);
110
+ }
111
+ }
112
+ return res. toArray(new String [0 ]);
113
+ }
114
+ }
115
+ ```
116
+
117
+ ### ** C++**
118
+
119
+ ``` cpp
120
+ class Solution {
121
+ public:
122
+ vector<string > findWords(vector<string >& words) {
123
+ string s = "12210111011122000010020202";
124
+ vector<string > ans;
125
+ for (auto& word : words)
126
+ {
127
+ unordered_set<char > t;
128
+ for (char c : word) t.insert(s[ tolower(c) - 'a'] );
129
+ if (t.size() == 1) ans.push_back(word);
130
+ }
131
+ return ans;
132
+ }
133
+ };
134
+ ```
135
+
136
+ ### **Go**
137
+
138
+ ```go
139
+ func findWords(words []string) []string {
140
+ s := "12210111011122000010020202"
141
+ var ans []string
142
+ for _, word := range words {
143
+ t := make(map[byte]bool)
144
+ for _, c := range word {
145
+ t[s[unicode.ToLower(c)-'a']] = true
146
+ }
147
+ if len(t) == 1 {
148
+ ans = append(ans, word)
149
+ }
150
+ }
151
+ return ans
152
+ }
153
+ ```
154
+
99
155
### ** ...**
100
156
101
157
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<string> findWords (vector<string>& words) {
4
+ string s = " 12210111011122000010020202" ;
5
+ vector<string> ans;
6
+ for (auto & word : words)
7
+ {
8
+ unordered_set<char > t;
9
+ for (char c : word) t.insert (s[tolower (c) - ' a' ]);
10
+ if (t.size () == 1 ) ans.push_back (word);
11
+ }
12
+ return ans;
13
+ }
14
+ };
Original file line number Diff line number Diff line change
1
+ func findWords (words []string ) []string {
2
+ s := "12210111011122000010020202"
3
+ var ans []string
4
+ for _ , word := range words {
5
+ t := make (map [byte ]bool )
6
+ for _ , c := range word {
7
+ t [s [unicode .ToLower (c )- 'a' ]] = true
8
+ }
9
+ if len (t ) == 1 {
10
+ ans = append (ans , word )
11
+ }
12
+ }
13
+ return ans
14
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public String [] findWords (String [] words ) {
3
- String s1 = "qwertyuiopQWERTYUIOP" ;
4
- String s2 = "asdfghjklASDFGHJKL" ;
5
- String s3 = "zxcvbnmZXCVBNM" ;
3
+ String s = "12210111011122000010020202" ;
6
4
List <String > res = new ArrayList <>();
7
5
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 ;
17
- }
6
+ Set <Character > t = new HashSet <>();
7
+ for (char c : word .toLowerCase ().toCharArray ()) {
8
+ t .add (s .charAt (c - 'a' ));
18
9
}
19
- if (n1 == n || n2 == n || n3 == n ) {
10
+ if (t . size () == 1 ) {
20
11
res .add (word );
21
12
}
22
13
}
You can’t perform that action at this time.
0 commit comments