当前位置:网站首页>500. 键盘行
500. 键盘行
2022-07-02 05:50:00 【有时候。】
1. 题目描述
给你一个字符串数组 words ,只返回可以使用在 美式键盘 同一行的字母打印出来的单词。键盘如下图所示。
美式键盘 中:
第一行由字符 “qwertyuiop” 组成。
第二行由字符 “asdfghjkl” 组成。
第三行由字符 “zxcvbnm” 组成。
示例:
输入:words = [“Hello”,“Alaska”,“Dad”,“Peace”]
输出:[“Alaska”,“Dad”]
输入:words = [“adsdf”,“sfd”]
输出:[“adsdf”,“sfd”]
2. 解题思路
对于输入的单词,先将其转换为小写,再判断组成单词的每个字母是否为某一行的子集即可。
- 在python中可以通过集合来快速判断组成单词的字母是否为某一行的子集
class Solution:
def findWords(self, words: List[str]) -> List[str]:
line1 = set('qwertyuiop')
line2 = set('asdfghjkl')
line3 = set('zxcvbnm')
def f(x):
x = set(x.lower())
return x.issubset(line1) or x.issubset(line2) or x.issubset(line3)
return list(filter(f, words))
- 在C++中依次判断单词的每个字母是否均在某一行中即可
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> res;
string line1 = "qwertyuiopQWERTYUIOP";
string line2 = "asdfghjklASDFGHJKL";
string line3 = "zxcvbnmZXCVBNM";
for(int i=0;i<words.size();i++) {
int n1 = 0, n2 = 0, n3 = 0;
int length = words[i].length();
for(int j=0;j<length;j++) {
if(line1.find(words[i][j]) != string::npos) {
n1++;
}else if(line2.find(words[i][j]) != string::npos) {
n2++;
}else if(line3.find(words[i][j]) != string::npos) {
n3++;
}
}
if(n1 == length || n2 == length || n3 == length) {
res.push_back(words[i]);
}
}
return res;
}
};
3. 知识点复习
- python–集合
- python–filter函数
边栏推荐
猜你喜欢

Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash

Fabric. JS activation input box

File contains vulnerability (I)

idea开发工具常用的插件合集汇总

2022-2-14 learning xiangniuke project - Section 6 displays login information

KMP idea and template code

Record sentry's path of stepping on the pit

The Hong Kong Stock Exchange learned from US stocks and pushed spac: the follow-up of many PE companies could not hide the embarrassment of the world's worst stock market

mysql事务和隔离级别

PHP 开发与测试 Webservice(SOAP)-Win
随机推荐
Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
Fabric. JS iText superscript and subscript
Fundamentals of software testing
Zzuli:1064 encrypted characters
php内的addChild()、addAttribute()函数
Centos8 installation mysql8.0.22 tutorial
Sliding window on the learning road
如何写出好代码 — 防御式编程指南
2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions
【技术随记-08】
2022-2-14 learning xiangniuke project - Section 7 account setting
Applet jumps to official account
Fabric. JS activation input box
Matplotlib double Y axis + adjust legend position
Yyds dry inventory what is test driven development
kmp思想及模板代码
《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
Zzuli:1065 count the number of numeric characters
文件包含漏洞(二)
Visual studio import