Fork me on GitHub

Leetcode500.Keyboard Row键盘行

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

 

示例:

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

 

注意:

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

 

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        map<char, int> check;
        check['Q'] = 1;check['q'] = 1;
        check['W'] = 1;check['w'] = 1;
        check['E'] = 1;check['e'] = 1;
        check['R'] = 1;check['r'] = 1;
        check['T'] = 1;check['t'] = 1;
        check['Y'] = 1;check['y'] = 1;
        check['U'] = 1;check['u'] = 1;
        check['I'] = 1;check['i'] = 1;
        check['O'] = 1;check['o'] = 1;
        check['P'] = 1;check['p'] = 1;
        check['A'] = 2;check['a'] = 2;
        check['S'] = 2;check['s'] = 2;
        check['D'] = 2;check['d'] = 2;
        check['F'] = 2;check['f'] = 2;
        check['G'] = 2;check['g'] = 2;
        check['H'] = 2;check['h'] = 2;
        check['J'] = 2;check['j'] = 2;
        check['K'] = 2;check['k'] = 2;
        check['L'] = 2;check['l'] = 2;
        check['Z'] = 3;check['z'] = 3;
        check['X'] = 3;check['x'] = 3;
        check['C'] = 3;check['c'] = 3;
        check['V'] = 3;check['v'] = 3;
        check['B'] = 3;check['b'] = 3;
        check['N'] = 3;check['n'] = 3;
        check['M'] = 3;check['m'] = 3;
        vector<string> res;
        int len = words.size();
        for(int i = 0; i < len; i++)
        {
            int flag = true;
            int temp = check[words[i][0]];
            for(int j = 0; j < words[i].size(); j++)
            {
                if(check[words[i][j]] != temp)
                {
                    flag =false;
                    break;
                }
            }
            if(flag)
                res.push_back(words[i]);
        }
        return res;
    }
};

 

posted @ 2018-10-26 22:42  lMonster81  阅读(103)  评论(0编辑  收藏  举报
/*评论*/ /*top按钮*/

/* 网易云控件 */