LeetCode——500. Keyboard Row

LeetCode——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.

American keyboard

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.

原题链接:https://leetcode.com/problems/keyboard-row/?tab=Description

分析

给出n个字符串,从而判断每个字符串中的字符是否来自美式键盘上的同一行,若来自同一行,返回该string。逐个word去比较即可。

C++

将键盘上的每行字符存储到相应的vector或者数组中,然后循环Input中的每个string,并且循环string中的每个char,从而进行比较。

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        unordered_set<char> set1 = {'q','w','e','r','t','y','u','i','o','p'};  
        unordered_set<char> set2 = {'s','d','f','g','h','j','k','l','a'};  
        unordered_set<char> set3 = {'z','x','c','v','b','n','m'};  
        vector<unordered_set<char>> sets = {set1, set2, set3};  
        vector<string> result; 

        for (int i = 0; i < words.size(); i++){
            //将word转化为小写
            string lower_words = words[i];
            transform(lower_words.begin(),lower_words.end(),lower_words.begin(), ::tolower);

            //先从words的第一个字符看是第几行的
            int index = 0;
            if (set1.find(lower_words[0]) != set1.end())
                index = 0;
            else if (set2.find(lower_words[0]) != set2.end())
                index = 1;
            else if (set3.find(lower_words[0]) != set3.end())
                index = 2;

            //保存第一次字符所在的行
            unordered_set<char> tmpSet = sets[index];
            int flag = 0;
            //遍历word的每一个字符,如果有一个字符没在第一个字符所在行的set中找到,就退出
            for (char c:lower_words){
                if(tmpSet.find(c) == tmpSet.end()){
                    flag = 1;
                    break;
                }
            }
            if (!flag) 
                result.push_back(words[i]);  
        }
        return result;
    }
};

C++的Standard Library并没有提供将std::string转成大写和小写的功能,只有在提供将char转成大写(toupper)和小写(tolower)的功能。可以利用STL的transform配合toupper/tolower,完成std::string转换大(小)写的功能。

或者在set中把大小写字符都罗列一遍。

Python

利用set的特性,判断元素是否在集合中

class Solution(object):
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        rs = map(set, ['qwertyuiop','asdfghjkl','zxcvbnm'])
        ans = []
        for word in words:
            wset = set(word.lower())
            if any(wset <= rset for rset in rs):
                ans.append(word)
        return ans

map

map(function, iterable, ...)

对可迭代函数’iterable’中的每一个元素应用‘function’方法,将结果作为list返回。

更多:https://my.oschina.net/zyzzy/blog/115096

set

set是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素。无法通过数字进行索引。

>>> set('abbac')
set(['a', 'c', 'b'])
s.issubset(t)  
s <= t  
测试是否 s 中的每一个元素都在 t 中  

s.issuperset(t)  
s >= t  
测试是否 t 中的每一个元素都在 s 中 

更多:http://blog.csdn.net/business122/article/details/7541486

any

any(...)
    any(iterable) -> bool

    Return True if bool(x) is True for any x in the iterable.
    If the iterable is empty, return False.

iterable的任何元素都为False、0,”,或者iterable全为空,则any(iterable)为False,否则为True

all():”有‘假’为False,全‘真’为True,iterable为空是True”
any():”有‘真’为True,全‘假’为False,iterable为空是False”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值