首页 > 其他 > 详细

500. Keyboard Row

时间:2017-07-27 21:57:03      阅读:247      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!