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:
You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet. 这道题描述的是:
给定一些单词,让我们找出这些单词中哪些单词是 所有字母都在键盘同一行上 qwertyuiop是键盘的第一行
asdfghjkl是键盘的第二行
zxcvbnm是键盘第三行 刚拿到这道题还真的很头疼- -难道要对单词每个字母进行遍历吗?? 后来参考了其他大神的思想,大致是两种思路,一种是正则表达式匹配,另一种是集合思想。
我用了集合的思想。用三个集合分别存下键盘个行的所有字母。
当给定一个单词 我们看看这个单词是不是这三个集合某一个的子集,如果是,那这个单词就满足字母都在一行 我的代码:
 class Solution(object):
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
q,a,z = set("qwertyuiop"), set("asdfghjkl"), set("zxcvbnm")
res = []
for str in words:
lset = set(str.lower() )
if lset.issubset(q) or lset.issubset(a) or lset.issubset(z):
res.append(str)
return res if __name__ == '__main__':
s = Solution()
res = s.findWords(["Hello", "Alaska", "Dad", "Peace"])
print(res)

leetcode算法: Keyboard Row的更多相关文章

  1. 46. leetcode 500. Keyboard Row

    500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabe ...

  2. Leetcode#500. Keyboard Row(键盘行)

    题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...

  3. LeetCode 500. Keyboard Row (键盘行)

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  4. [LeetCode] 500. Keyboard Row 键盘行

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  5. LeetCode 500 Keyboard Row 解题报告

    题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  6. LeetCode: 500 Keyboard Row (easy)

    题目: Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  7. LeetCode算法题-Keyboard Row(Java实现)

    这是悦乐书的第245次更新,第258篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第112题(顺位题号是500).给定一个单词列表,返回可以在美国键盘的一行上使用字母表键 ...

  8. LeetCode——Keyboard Row

    LeetCode--Keyboard Row Question Given a List of words, return the words that can be typed using lett ...

  9. leetcode算法: Find Bottom Left Tree Value

    leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...

  10. LeetCode算法题-Image Smoother(Java实现)

    这是悦乐书的第282次更新,第299篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第150题(顺位题号是661).给定表示图像灰度的2D整数矩阵M,您需要设计一个平滑器以 ...

随机推荐

  1. epel扩展库的安装

    epel扩展库的安装 2017-10-09  18:07:48 个人原创,转载请注明作者,出处,否则追究法律责任 1,centos6.x系统中,必需安装epel-release-6-8.noarch. ...

  2. python趣味 ——奇葩的全局形参

    在c++,c#,js等语言中: 函数定义(参数) 函数体:参数修改 这里的参数修改都是仅限于这个函数体内的 python不知道是不是bug,我们这样写: def test(a=[]): a.appen ...

  3. FastJson简单使用

    首先建立两个实体类,Student.java 和 Teacher.java public class Student { private int id; private String name; pr ...

  4. UWP:可滚动的PivotHeader

    UWP开发里,Pivot真是个令人又爱又恨的控件.为了实现某些可滚动Header的效果,有些大佬甚至去掉了原本的Header,使用一个ListView或者ListBox自己画Header,不过这样会让 ...

  5. 笔记:MyBatis 使用 Java API配置

    我们已经讨论了各种MyBatis配置元素,如envronments.typeAlias和typeHandlers,以及如何使用XML配置它们.即使你想使用基于JavaAPI的MyBatis配置,MyB ...

  6. Matlab绘图基础——绘制三维曲线

    %% 绘制三维曲线 %plot3函数,其中每一组x,y,z组成一组曲线的坐标参数,选项的定义和plot函数相同. %1.当x,y,z是同维向量时,则x,y,z 对应元素构成一条三维曲线. x0 = 0 ...

  7. javaScript设计模式 -- 灵活的javaScript语言

    因为好长时间的懒惰和懈怠,好久没有更新文章了,从现在开始我会按时更新一些自己总结的一些知识,和研究的东西,希望能让大家从我这里学到一点点的知识. 本文参考了张荣铭的javascript设计模式一书,算 ...

  8. SpringBoot03 项目热部署

    1 问题 在编写springBoot项目时,经常需要修改代码:但是每次修改代码后都需重新启动,修改的代码才会生效 2 这么实现IDEA能够像Eclipse那样保存过后就可以自动进行刷新呢 将sprin ...

  9. 数据库(Oracle)运维工作内容及常用脚本命令

    1.系统资源状况:--内存及CPU资源  --linux,solaris,aix    vmstat 5  --说明:    1)观察空闲内存的数量多少,以及空闲内存量是否稳定,如果不稳定就得想办法来 ...

  10. 【RabbitMQ系列】 Spring mvc整合RabbitMQ

    一.linux下安装rabbitmq 1.安装erlang环境 wget http://erlang.org/download/otp_src_18.2.1.tar.gz tar xvfz otp_s ...