首页 > 其他 > 详细

【LeetCode】水题(刚开始重新刷题找感觉用的)

时间:2018-07-02 21:15:22      阅读:171      评论:0      收藏:0      [点我收藏+]

[500]  Keyboard Row [Easy]

给N个单词,判断哪些单词的字母在键盘的同一行,输出这些单词。

技术分享图片
 1 class Solution {
 2 public:
 3     set<char> setLine1{q, w, e, r, t, y, u, i, o, p}, 
 4             setLine2{a, s, d, f, g, h, j, k, l},
 5             setLine3{z, x, c, v, b, n, m};
 6 
 7     int findGroup(char lowerChar) {
 8         if (setLine1.find(lowerChar) != setLine1.end()) {
 9             return 1;
10         } else if (setLine2.find(lowerChar) != setLine2.end()) {
11             return 2;
12         } else if (setLine3.find(lowerChar) != setLine3.end()){
13             return 3;
14         }
15         return -1;
16     }
17     
18     vector<string> findWords(vector<string>& words) {
19         vector<string> answer;
20         for(auto word : words) {
21             int groupId = 0;
22             bool ansIn = true;
23             groupId = isupper(word[0]) ? findGroup(tolower(word[0])) : findGroup(word[0]);
24             for (auto i = 1; i < word.size(); ++i) {
25                 int tmpGroupId = 0;
26                 tmpGroupId = isupper(word[i]) ? findGroup(tolower(word[i])) : findGroup(word[i]);
27                 if (tmpGroupId != groupId) {
28                     ansIn = false;
29                     break;
30                 }
31             }
32             if (ansIn) {
33                 answer.push_back(word);
34             }
35         }
36         return answer;
37     }
38 };
View Code

 

【LeetCode】水题(刚开始重新刷题找感觉用的)

原文:https://www.cnblogs.com/zhangwanying/p/9255749.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!