500. Keyboard Row (5月26日)

解答

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        vector<string> result;
        string first{"qwertyuiopQWERTYUIOP"};
        string second{"asdfghjklASDFGHJKL"};
        string third{"zxcvbnmZXCVBNM"};
        for(auto it=words.begin();it!=words.end();++it){
            bool flag1=findchar(first,*it);
            bool flag2=findchar(second,*it);
            bool flag3=findchar(third,*it);
            if(flag1==true||flag2==true||flag3==true){
                 result.push_back(*it);
            }
        }
        return result;
    }
    bool findchar(const string & str,const string & goal){
        for(auto ch:goal){
            if(str.find(ch)==string::npos){
                return false;
            }
        }
        return true;
    }
};

猜你喜欢

转载自www.cnblogs.com/cs-niaocai/p/9094030.html