leetcode算法: 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:
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的更多相关文章
- 46. leetcode 500. Keyboard Row
500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabe ...
- Leetcode#500. Keyboard Row(键盘行)
题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...
- LeetCode 500. Keyboard Row (键盘行)
Given a List of words, return the words that can be typed using letters of alphabet on only one 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' ...
- LeetCode 500 Keyboard Row 解题报告
题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...
- LeetCode: 500 Keyboard Row (easy)
题目: Given a List of words, return the words that can be typed using letters of alphabet on only one ...
- LeetCode算法题-Keyboard Row(Java实现)
这是悦乐书的第245次更新,第258篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第112题(顺位题号是500).给定一个单词列表,返回可以在美国键盘的一行上使用字母表键 ...
- LeetCode——Keyboard Row
LeetCode--Keyboard Row Question Given a List of words, return the words that can be typed using lett ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- LeetCode算法题-Image Smoother(Java实现)
这是悦乐书的第282次更新,第299篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第150题(顺位题号是661).给定表示图像灰度的2D整数矩阵M,您需要设计一个平滑器以 ...
随机推荐
- .Net调用钉钉接口,实现发送企业消息功能
我在钉钉官网上看了下,关于调用钉钉接口,发送企业消息通知,他给的核心代码总共几行. 用到的一个接口引用 TopSDK.dll 下载网址https://open-doc.dingtalk.com/doc ...
- 基于docer搭建私有gitlab服务器
今天闲着无聊,于是乎想用最近很流行的docker容器搭建一个自己的gitlab的服务器,关于docker和gitlab就不多介绍了,网上查了很多资料,貌似没有一个统一的方法,很乱很杂,而且很容易误导人 ...
- Python基础-week01
本节内容摘要:http://www.cnblogs.com/Jame-mei Python介绍 Python是怎么样的语言? Python 2 or 3? 安装 Hello World程序 变量 用户 ...
- 简单使用git和github来管理代码----配置与使用
在以前没听说过github之前,自己写的代码很容易丢或者遗失,等到用时才知码到用时方恨丢,现在用了github,真的是替自己生省不少的事,闲话不多说,上教程. 1 在github上注册账号 https ...
- python 对模块的应用你还得练点这些
1.有如下字符串:n = "路飞学城"(编程题) - 将字符串转换成utf-8的字符编码的字节,再将转换的字节重新转换为utf-8的字符编码的字符串 - 将字符串转换成gbk的字符 ...
- Object.prototype.hasOwnProperty与Object.getOwnPropertyNames
Object.prototype.hasOwnProperty() 所有继承了 Object 的对象都会继承到 hasOwnProperty 方法.这个方法可以用来检测一个对象是否含有特定的自身属性: ...
- react+redux+webpack+git技术栈
一.git bash here mdkr cnpm init -y ls -a ls -l ls -la隐藏的也可查看 cat package.json 二.npm npm i webpack-dev ...
- table 表格的增删和修改
如上图,图片的增删都没有问题:唯一的问题就是我改变下一行的内容时,把上面一行给覆盖了,费了好久,终于找到原因了,直接贴代码: 效果如下:
- C语言描述链表的实现及操作
一.链表的创建操作 // 操作系统 win 8.1 // 编译环境 Visual Stuido 2017 #include<stdio.h> #include<malloc.h> ...
- easyUi五个常用插件
1.对话框(Dialog) 对话框(dialog)是一个特殊类型的窗口,它在顶部有一个工具栏,在底部有一个按钮栏.默认情况下,对话框(dialog)只有一个显示在头部右侧的关闭工具.用户可以配置对话框 ...