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,您需要设计一个平滑器以 ...
随机推荐
- xlsx导入成--json
这两天遇到大难题了,就是这个 xlsx 导入问题,之前用的xlsx.full.min.js,写的导入,结果不兼容ie浏览器,研究这个也好长时间,网上居然还没有搜到合适的,自己写从xlsx官网上 ...
- Maven-10: 使用maven-help-plugin描述插件
除了访问在线的插件文档之外,还可以借助maven-help-plugin来获取插件的详细信息.可以运行如下命令来获取maven-compiler-plugin2.1版本的信息: [INFO] Scan ...
- 【Python】 python对象的文件化 pickle
pickle 之前隐隐约约在哪里看到过pickle这个模块但一直没怎么用过.然后让我下定决心学习一下这个模块的原因竟然是[妹抖龙女(男)主在工作中用到了pickle哈哈哈].嗯嗯,不扯皮了.pickl ...
- STL --> set用法
set用法 一.set和multiset基础 set和multiset会根据特定的排序准则,自动将元素进行排序.不同的是后者允许元素重复而前者不允许. 需要包含头文件: #include <se ...
- shell队列实现线程并发控制(转)
需求:并发检测1000台web服务器状态(或者并发为1000台web服务器分发文件等)如何用shell实现? 方案一:(这应该是大多数人都第一时间想到的方法吧) 思路:一个for循环1000次,顺序执 ...
- php设计模式八-----装饰器模式
1.介绍: 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
- C#添加背景音乐
<MediaElement Name="audio"/> <Button Name="music" Content="点我有音乐哦& ...
- JavaScript(第十六天)【BOM基础】
学习要点: 1.window对象 2.location对象 3.history对象 BOM也叫浏览器对象模型,它提供了很多对象,用于访问浏览器的功能.BOM缺少规范,每个浏览器提供商又按照自己想法 ...
- beta冲刺3
一,昨天的问题: 页面整理还没做 我的社团这边的后台数据库未完成,前端代码修改未完成. 二,今天已完成 页面整理基本完成,把登陆独立出来了,然后基本处理掉了多余页面(反正也没几个--) 我的社团这边试 ...
- Beta Scrum Day 6
听说