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

500. 键盘行

时间:2018-05-10 00:07:33      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:.com   ati   img   ble   can   set   src   图片   png   

给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。

 

技术分享图片

 

示例1:

输入: ["Hello", "Alaska", "Dad", "Peace"]
输出: ["Alaska", "Dad"]

注意:

  1. 你可以重复使用键盘上同一字符。
  2. 你可以假设输入的字符串将只包含字母。

 

收获:

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 };

 

500. 键盘行

标签:.com   ati   img   ble   can   set   src   图片   png   

原文地址:https://www.cnblogs.com/jj81/p/9017276.html

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