Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6098e36

Browse files
Add files via upload
1 parent e6a0e95 commit 6098e36

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

‎Keyboard Row/Keyboard_Row.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Runtime: 0 ms, faster than 100.00% of C++ online submissions for Keyboard Row.
2+
// Memory Usage: 8.6 MB, less than 57.14% of C++ online submissions for Keyboard Row.
3+
4+
class Solution
5+
{
6+
public:
7+
vector<string> findWords(vector<string>& words)
8+
{
9+
vector<string> res;
10+
11+
vector<int> memo(26, 0);
12+
13+
// 从上往下数第二层
14+
memo['a' - 'a'] = 1;
15+
memo['s' - 'a'] = 1;
16+
memo['d' - 'a'] = 1;
17+
memo['f' - 'a'] = 1;
18+
memo['g' - 'a'] = 1;
19+
memo['h' - 'a'] = 1;
20+
memo['j' - 'a'] = 1;
21+
memo['k' - 'a'] = 1;
22+
memo['l' - 'a'] = 1;
23+
24+
// 从上往下数第三层
25+
memo['z' - 'a'] = 2;
26+
memo['x' - 'a'] = 2;
27+
memo['c' - 'a'] = 2;
28+
memo['v' - 'a'] = 2;
29+
memo['b' - 'a'] = 2;
30+
memo['n' - 'a'] = 2;
31+
memo['m' - 'a'] = 2;
32+
33+
for (string word : words)
34+
{
35+
bool flag = true;
36+
37+
if (word.length() != 0)
38+
{
39+
char temp = word[0] >= 'a' && word[0] <= 'z' ? word[0] : word[0] + 32;
40+
int layer = memo[temp - 'a'];
41+
42+
for (char chr : word)
43+
{
44+
chr = chr >= 'a' && chr <= 'z' ? chr : chr + 32;
45+
if (memo[chr - 'a'] != layer)
46+
{
47+
flag = false;
48+
break;
49+
}
50+
}
51+
}
52+
53+
if (flag) res.push_back(word);
54+
}
55+
56+
return res;
57+
}
58+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /