当前位置:网站首页>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函数
边栏推荐
- Fabric. JS iText sets the color and background color of the specified text
- Go language web development is very simple: use templates to separate views from logic
- 正则表达式总结
- Sliding window on the learning road
- PHP development and testing WebService (soap) -win
- Visual Studio导入
- Zzuli:1064 encrypted characters
- 2022-2-14 learning xiangniuke project - Section 6 displays login information
- File contains vulnerability (I)
- Generate QR code
猜你喜欢
"Simple" infinite magic cube
kmp思想及模板代码
[golang syntax] be careful with the copy of slices
Importation de studio visuel
Installation du tutoriel MySQL 8.0.22 par centos8
PHP 开发与测试 Webservice(SOAP)-Win
Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
软件测试答疑篇
【技术随记-08】
RGB infinite cube (advanced version)
随机推荐
Straighten elements (with transition animation)
Sliding window on the learning road
Centos8 installation mysql8.0.22 tutorial
File contains vulnerability (I)
Yyds dry inventory what is test driven development
Reflection of the soul of the frame (important knowledge)
简单封装 js并应用
【論文翻譯】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
Fabric. JS iText sets the color and background color of the specified text
JVM class loading mechanism
idea开发工具常用的插件合集汇总
Fabric. JS activation input box
Reading notes of cgnf: conditional graph neural fields
Record sentry's path of stepping on the pit
VSCode paste image插件保存图片路径设置
Visual Studio導入
Cube magique infini "simple"
How to write good code - Defensive Programming Guide
[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
[golang syntax] be careful with the copy of slices