500. 键盘行
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。
示例1:
输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"]
注意:
- 你可以重复使用键盘上同一字符。
- 你可以假设输入的字符串将只包含字母。
收获:
std::unordered_set
http://classfoo.com/ccby/article/qNNOJ
1 class Solution { 2 public: 3 vector<string> findWords(vector<string>& words) { 4 vector<string> res; 5 unordered_set<char> row1 {'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'}; 6 unordered_set<char> row2{'a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L'}; 7 unordered_set<char> row3{'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'}; 8 for(string word : words) { 9 int a = 0; 10 int b = 0; 11 int c = 0; 12 for(char ch : word) { 13 if(row1.count(ch)) a = 1; 14 else if(row2.count(ch)) b = 1; 15 else if(row3.count(ch)) c = 1; 16 17 if(a + b + c > 1) break; 18 } 19 if(a + b + c == 1) res.push_back(word); 20 } 21 22 return res; 23 } 24 };