500. Keyboard Row

Posted いいえ敗者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了500. Keyboard Row相关的知识,希望对你有一定的参考价值。

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"]

.随便写了份代码提交 ac了,但是我知道我写错了,因为我没有把单词变成小写去判断,可见他们数据出水了

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        string s1("qwertyuiop");
        string s2("asdfghjkl");
        string s3("zxcvbnm");
        map<char, int>mp;
        int l1 = s1.length();
        int l2 = s2.length();
        int l3 = s3.length();
        int n = words.size();
        vector<string> v;
        for (int i = 0; i < l1; ++i) mp[s1[i]] = 1;
        for (int i = 0; i < l2; ++i) mp[s2[i]] = 2;
        for (int i = 0; i < l3; ++i) mp[s3[i]] = 3;
        for (int i = 0; i < n; ++i) {
            int a = 0, b = 0, c = 0, kas = 0;
            for(auto x : words[i]) {
                char y = tolower(x);
                if (mp[y] == 1) {
                    if (b || c) break;
                    a = 1;
                }
                else if (mp[y] == 2) {
                    if (a || c) break;
                    b = 1;
                }
                else if (mp[y] == 3) {
                    if (a || b) break;
                    c = 1;
                }
                kas++;
            }
            if (words[i].size() == kas) v.push_back(words[i]);
        }
        return v;
    }
};

 

以上是关于500. Keyboard Row的主要内容,如果未能解决你的问题,请参考以下文章

500. Keyboard Row

500. Keyboard Row

500. Keyboard Row

LeetCode-500. Keyboard Row

LeetCode. 500. Keyboard Row

leetcode Keyboard Row500 Java