码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-02 21:36:48      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:code   ring   lse   group   img   amp   word.size   lap   auto   

[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】水题(刚开始重新刷题找感觉用的)

标签:code   ring   lse   group   img   amp   word.size   lap   auto   

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

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!