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

500. Keyboard Row

时间:2017-07-27 22:38:51      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:board   input   vector   put   str   find   for   har   ace   

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

标签:board   input   vector   put   str   find   for   har   ace   

原文地址:http://www.cnblogs.com/pk28/p/7247442.html

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