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,您需要设计一个平滑器以 ...
随机推荐
- Spring源码学习:第0步--环境准备
Spring源码现在已托管于GitHub,相比于以前直接从官网下载一个压缩包的方式来说,确实方便了不少. GitHub地址:https://github.com/spring-projects/spr ...
- 关闭NetworkManager的作用
author: headsen chen date: 2017-11-21 13:34:23 个人原创 重启网卡后,会造成网卡失效,报错如下: Bringing up interface eth0 ...
- Redis 学习相关的网站
Redis 命令参考 http://doc.redisfans.com/ https://redis.io/commands http://www.redis.net.cn Redis教程 http: ...
- 【django之分页器】
一.什么是分页功能 二.Django的分页器(paginator) 语法: paginator = Paginator(book_list, 8) #8条一页print("count:&qu ...
- 员工选票系统-java
Yuangong.java package com.toupiao; public class Yuangong { private String name; private int piao; pu ...
- poj-1045(数学不好怪我咯)
Description Consider the AC circuit below. We will assume that the circuit is in steady-state. ...
- SpringBoot 自定义Banner
在2016年的最后一天,借用Spring Boot的Banner向各位程序猿同仁们问候一声:Happy New Year. 接下来我们就来介绍一下这个轻松愉快的自定义banner功能.实现的方式非常简 ...
- k8s实战为aspnetcore.webapi微服务注入配置信息 - kubernetes
1.浅析k8s配置信息 Secret 以密文的形式存储数据,可以用来保存一些敏感信息,例如:OAuth tokens.私钥.密码.数据库连接.事件总线连接等. ConfigMap 以明文的形式存储数据 ...
- 【Python】 高级文件操作 shutil
shutil 很多时候,我想要对文件进行重命名,删除,创建等操作的时候的想法就是用subprocess开一个子进程来处理,但是实际上shutil可以更加方便地提供os的文件操作接口,从而可以一条语句搞 ...
- 线程池与Python中的GIL
线程池是一个操作系统的概念,它是对多线程的一种优化. 多线程的时候,创建和销毁线程伴随着操作系统的开销,如果频繁创建/销毁线程,则会使效率大大降低. 而线程池,是先创建出一批线程放入池子里,需要创建线 ...