标签: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 };
标签:code ring lse group img amp word.size lap auto
原文地址:https://www.cnblogs.com/zhangwanying/p/9255749.html