LeetCode水题(刚开始重新刷题找感觉用的)
Posted zhangwanying
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode水题(刚开始重新刷题找感觉用的)相关的知识,希望对你有一定的参考价值。
[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 };
以上是关于LeetCode水题(刚开始重新刷题找感觉用的)的主要内容,如果未能解决你的问题,请参考以下文章