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. 【django之博客系统开发】

    一.项目简介 使用django开发一套博客系统,参考博客园. 需求如下: 项目结构: 二.全部代码 from django.db import models # Create your models ...

  2. [POJ 2226] Muddy Fields

    题目 Description 如何放木板保证只覆盖到 '*' 而没有覆盖到 '.' Solution (我太废了竟然想这题想了一个小时)考虑当前需要被覆盖的点 (x,y),假设有一块横着铺的木板 i ...

  3. 笔记:MyBatis XML配置-typeAliases 内建别名表

    别名 映射的类型 _byte byte _long long _short short _int int _integer int _double double _float float _boole ...

  4. Spring Clould负载均衡重要组件:Ribbon中重要类的用法

    Ribbon是Spring Cloud Netflix全家桶中负责负载均衡的组件,它是一组类库的集合.通过Ribbon,程序员能在不涉及到具体实现细节的基础上"透明"地用到负载均衡 ...

  5. 关于mongodb按照字段模糊查询方法

    模糊查询:tname包含某个关键字测试' cd /opt/soft/mongodb/bin ./mongo --host 192.168.0.1  --port 17017  test db.test ...

  6. nginx域名跳转技巧

    1.地址重写:访问server_name的时候跳转到http://www.cnblogs.com/qinyujie/ 修改nginx配置文件.加入到server{...}字段或者location字段里 ...

  7. Jmeter中正则表达式提取器使用详解

    在使用Jmeter过程中,会经常使用到正则表达式提取器提取器,虽然并不直接涉及到请求的测试,但是对于数据的传递起着很大的作用,本篇博文就是主要讲解关于正则表达式及其在Jmeter的Sampler中的调 ...

  8. Idea  调试代码

    ---恢复内容开始--- set DEBUG_PORT=8787 set JAVA_DEBUG=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,addr ...

  9. 如何在win10查看wifi密码

    tep1 找到wifi图标 step 2 右键点击打开网络共享中心 没有啦!!

  10. Mac下安装virtualenv, 并在PyCharm中使用

    今天在安装一个leader写的package的时候,同事建议安装到虚拟环境中,再在PyCharm里使用该虚拟环境即可.此处记录下经过: 开发Python应用的时候,有时会遇到不同的Python应用依赖 ...