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.

给出一系列的单词,判断这些单词是否出自键盘的同一行(忽略大小写)
键盘就是标准的美式键盘:
这里写图片描述
题目中给出的例子是这样的:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]

输入是一个list,输出是一个list,输出的list是所有字母在同一行的。我解决这到题目的思路是这样的:
1.将每一行的字母放一个字符串
2.遍历输入的list
3.字符串变成小写,
4.判断每一个字母属于哪一行,并将数字付给临时集合set
5.判断set的大小

具体的程序如下(python):

class Solution(object):
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        mylist = []
        first = "qwertyuiop"
        sencond = "asdfghjkl"
        threed = "zxcvbnm"

        for ii in range(len(words)):
            i = (words[ii].lower())
            temp = []
            for j in range(len(i)):
                if i[j] in first:
                    temp.append(1)
                if i[j] in sencond:
                    temp.append(2)
                if i[j] in threed:
                    temp.append(3)
            if len(set(temp))==1:
                mylist.append(words[ii])
        return mylist

没有调整变量名(不规范),手写代码一次通过。其结果是这样的:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值