zoukankan      html  css  js  c++  java
  • 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;
        }
    };
    

  • 相关阅读:
    算法练习题
    牛XX面试题
    web前端面试题
    在js中什么情况下会返回undefined
    yarn
    Web 开发技巧
    dll 文件创建与使用
    【转】 VS2008在64位Windows平台上编译ArcEngine导致HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)错误的解决办法
    Googlemap获取经纬度For APIV3
    javascript局部变量/全局变量
  • 原文地址:https://www.cnblogs.com/lMonster81/p/10434072.html
Copyright © 2011-2022 走看看