File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments