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

500. Keyboard Row

时间:2018-08-13 22:04:34      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:false   tput   ike   example   word.size   img   find   solution   分享图片   

题目描述:

Given a List of words, return the words that can be typed using letters of alphabet on only one row‘s of American keyboard like the image below.

 

技术分享图片

 

Example 1:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]

 

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

解题思路:

判断输入的每个单词中的字母是否都在键盘的同一行上。

代码:

 1 class Solution {
 2 public:
 3     vector<string> findWords(vector<string>& words) {
 4         vector<string> ret;
 5         for (auto word : words) {
 6             bool sig = true;
 7             char c = tolower(word[0]);
 8             int num = alpha[c];
 9             for (int i = 1; i < word.size(); ++i) {
10                 c = tolower(word[i]);
11                 if (alpha[c] != num) {
12                     sig = false;
13                     break;
14                 }
15             }
16             if (sig)
17                 ret.push_back(word);
18         }
19         return ret;
20     }
21     
22     unordered_map<char, int> alpha = {
23         {q, 1}, {w, 1}, {e, 1}, {r, 1}, {t, 1}, {y, 1}, {u, 1}, {i, 1}, {o, 1}, {p, 1},
24         {a, 2}, {s, 2}, {d, 2}, {f, 2}, {g, 2}, {h, 2}, {j, 2}, {k, 2}, {l, 2},
25         {z, 3}, {x, 3}, {c, 3}, {v, 3}, {b, 3}, {n, 3}, {m ,3}
26     };
27 };

 

500. Keyboard Row

标签:false   tput   ike   example   word.size   img   find   solution   分享图片   

原文地址:https://www.cnblogs.com/gsz-/p/9471125.html

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