[LeetCode]500. Keyboard Row(输出在键盘一行上能敲出来的单词)

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”]
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.(你可以假定输入字符串只包含字母
    字母)

这里写图片描述

自己考虑得太复杂了,只好百度了一下,希望还能有更好的解法
参考http://www.cnblogs.com/grandyang/p/6421749.html,表示感谢


class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        vector<string> res;
            unordered_set<char> row1{'q','w','e','r','t','y','u','i','o','p'};
            unordered_set<char> row2{'a','s','d','f','g','h','j','k','l'};
            unordered_set<char> row3{'z','x','c','v','b','n','m'};
            for (string word : words) {
                int one = 0, two = 0, three = 0;
                string s = word;
                int index=0;
                while(s[index]!='\0') {
                    if (s[index] < 'a') s[index] += 32;
                    if (row1.count(s[index])) one = 1;
                    if (row2.count(s[index])) two = 1;
                    if (row3.count(s[index])) three = 1;
                    if (one + two + three > 1) break;
                    cout << s[index++];
                }
                if (one + two + three == 1) res.push_back(word);
            }
            return res;
        }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值