[LeetCode]500. Keyboard Row

来源:互联网 发布:splice软件安卓 编辑:程序博客网 时间:2024/05/21 07:15

题目大意:给出单词,找出都只在键盘同一行的字母组成的单词。


解题思路:简单模拟,遍历每个单词的字母,看是否都在同一行。


class Solution {public:    vector<string> findWords(vector<string>& words) {        int len = words.size();        bool f1,f2,f3;        vector<string>res;        unordered_set<char> r1 = {'q','Q','w','W','e','E','r','R','t','T','y','Y','u','U','i','I','o','O','p','P'};        unordered_set<char> r2 = { 'a','A','s','S','d','D','f','F','g','G','h','H','j','J','k','K','l','L'};        unordered_set<char> r3 = { 'z','Z','x','X','c','C','v','V','b','B','n','N','m','M'};        for(auto &word:words)        {            f1 = f2 = f3 = true;            for(auto &i:word)            {                if(f1)                {                    auto it = r1.find(i);                    if(it==r1.end()) f1 = false;                }                if(f2)                {                    auto it = r2.find(i);                    if(it==r2.end()) f2 = false;                }                if(f3)                {                    auto it = r3.find(i);                    if(it==r3.end()) f3 = false;                }            }            if(f1 || f2 || f3) res.push_back(word);        }        return res;    }};


0 0