码迷,mamicode.com
首页 > 其他好文 > 详细

500. Keyboard Row

时间:2018-06-02 22:52:42      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:for   灵活   string   第一个   etl   nbsp   rds   span   int   

方法一,标志位,这里标志位的运用很灵活,可以学习一下

 1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     vector<string> findWords(vector<string>& words) 
12     {
13         unordered_set<char> setline1{Q,W,E,R,T,Y,U,I,O,P,q,w,e,r,t,y,u,i,o,p};
14         unordered_set<char> setline2{A,S,D,F,G,H,J,K,L,a,s,d,f,g,h,j,k,l};
15         unordered_set<char> setline3{Z,X,C,V,B,N,M,z,x,c,v,b,n,m};
16         vector<string> res;
17         for(string &s:words)
18         {
19             bool in1=true,in2=true,in3=true;
20             for(char &c:s)
21             {
22                 if(in1)
23                     if(setline1.find(c)==setline1.end())
24                         in1=false;
25                 if(in2)
26                     if(setline2.find(c)==setline2.end())
27                         in2=false;
28                 if(in3)
29                     if(setline3.find(c)==setline3.end())
30                         in3=false;
31             }
32             if(in1||in2||in3)
33                 res.push_back(s);
34         }
35         return res;
36     }
37 };

方法二:用第一个字符确定出查找的串,再在那个串中找

 1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     vector<string> findWords(vector<string>& words) 
12     {
13         unordered_set<char> setline1{Q,W,E,R,T,Y,U,I,O,P,q,w,e,r,t,y,u,i,o,p};
14         unordered_set<char> setline2{A,S,D,F,G,H,J,K,L,a,s,d,f,g,h,j,k,l};
15         unordered_set<char> setline3{Z,X,C,V,B,N,M,z,x,c,v,b,n,m};
16         vector<string> res;
17         for(string &s:words)
18         {
19             bool flag=true;
20             unordered_set<char> curset;
21             if(setline1.find(s[0])!=setline1.end())
22                 curset=setline1;
23             else if(setline2.find(s[0])!=setline2.end())
24                 curset=setline2;
25             else 
26                 curset=setline3;
27             for(char &c:s)
28                 if(curset.find(c)==curset.end())
29                 {
30                     flag=false;
31                     break;
32                 }
33             if(flag)
34                 res.push_back(s);
35         }
36         return res;
37     }
38 };

 

500. Keyboard Row

标签:for   灵活   string   第一个   etl   nbsp   rds   span   int   

原文地址:https://www.cnblogs.com/zhuangbijingdeboke/p/9127183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!