热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【LeetCode】500.KeyboardRow【E】【75】

GivenaListofwords,returnthewordsthatcanbetypedusinglettersofalphabetononlyone

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.

Subscribe to see which companies asked this question.


想法很简单 就是用集和运算 然后看每个word是不是一行的子集

最开始写的时候 太傻了 被注释掉了




class Solution(object):
    def findWords(self, words):
        #row1 = set(['Q','W','E','R','T','Y','U','I','O','P','q','w','e','r','t','y','u','i','o','p'])
        #row2 = set(['A','S','D','F','G','H','J','K','L','a','s','d','f','g','h','j','k','l'])
        #row3 = set(['Z','X','C','V','B','N','M','z','x','c','v','b','n','m'])
        row1 = set('qwertyuiop')
        row2 = set('asdfghjkl')
        row3 = set('zxcvbnm')

        res = []
        for i in words:
            si = set(i.lower())
            #if len(si - row1) == 0 or len(si - row2) == 0 or len(si - row3) == 0:
            if si.issubset(row1) or si.issubset(row2) or si.issubset(row3):
                res += i,
        return res
        """
        :type words: List[str]
        :rtype: List[str]
        """



推荐阅读
author-avatar
欢不是欢7
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有