Leetcode 500 Keyboard Row

来源:互联网 发布:斯诺登现状2017 知乎 编辑:程序博客网 时间:2024/05/16 14:39

Leetcode 500 Keyboard Row

#include <string>using namespace std;class Solution {public:    vector<string> findWords(vector<string>& words) {        string firstLine = "qwertyuiopQWERTYUIOP";        string secondLine = "asdfghjklASDFGHJKL";        string thirdLine = "zxcvbnmZXCVBNM";            vector<string> result;        int oneflag,secondflag,thirdflag = 0;       //c++ 11        for(auto& word : words)        {            for(auto& letter : word)            {                if(firstLine.find(letter) != firstLine.npos)//string::npos also is fine                    oneflag = 1;                else if(secondLine.find(letter) != string::npos)                    secondflag = 1;                else if(thirdLine.find(letter) != string::npos)                    thirdflag = 1;            }            if(oneflag + secondflag + thirdflag == 1)            {                result.push_back(word);              }            oneflag = 0;            secondflag = 0;            thirdflag = 0;        }        return result;    }};
原创粉丝点击