zoukankan      html  css  js  c++  java
  • 500. Keyboard Row

    Question:

    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.

    Solution:

    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            vector<string> result;
            vector<set<char>> v(3);
            string s1 = "QWERTYUIOPqwertyuiop", s2 = "ASDFGHJKLasdfghjkl", s3 = "ZXCVBNMzxcvbnm";
            for (int i = 0; i < s1.length(); i++) v[0].insert(s1[i]);
            for (int i = 0; i < s2.length(); i++) v[1].insert(s2[i]);
            for (int i = 0; i < s3.length(); i++) v[2].insert(s3[i]);
            for (int i = 0; i < words.size(); i++) {
                int tag = -1;
                bool flag = true;
                if (words[i].length() == 0) continue; 
                if (v[0].find(words[i][0]) != v[0].end()) tag = 0;
                if (v[1].find(words[i][0]) != v[1].end()) tag = 1;
                if (v[2].find(words[i][0]) != v[2].end()) tag = 2;
                for (int j = 1; j < words[i].length(); j++) {
                    if (v[tag].find(words[i][j]) == v[tag].end()) {
                        flag = false;
                        break;
                    }
                }
                if (flag == true)
                    result.push_back(words[i]);
            }
            return result;
        }
    };

    题目直达:https://leetcode.com/problems/keyboard-row/#/description

    答案直达:http://www.liuchuo.net/archives/3202

  • 相关阅读:
    day37 事务
    小组分享
    day36 pymysql 索引
    day 35 多表查询
    day 35 作业
    day 34 作业
    AST 节点类型对照表
    babel _shallowEqual.default
    js Proxy
    Symbol
  • 原文地址:https://www.cnblogs.com/SapphireCastle/p/6758053.html
Copyright © 2011-2022 走看看