当前位置:网站首页>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函数
边栏推荐
- “簡單”的無限魔方
- brew install * 失败,解决方法
- ThreadLocal memory leak
- Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
- Cube magique infini "simple"
- 运动健身的一些心得经验
- 青训营--数据库实操项目
- 2022-2-14 learning xiangniuke project - Section 7 account setting
- js判断移动端还是pc端
- "Simple" infinite magic cube
猜你喜欢
【论文翻译】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
Cube magique infini "simple"
KMP idea and template code
Importation de studio visuel
15 C language advanced dynamic memory management
Software testing - concept
Appnuim environment configuration and basic knowledge
5g market trend in 2020
kmp思想及模板代码
mysql事务和隔离级别
随机推荐
PHP inner class name is the same as the inner class method name
php读文件(读取文件内含有某字符串的指定行)
"Original, excellent and vulgar" in operation and maintenance work
Software testing learning - day 4
Pytorch Chinese document
ThreadLocal memory leak
mysql事务和隔离级别
1035 Password
File contains vulnerability (I)
Fabric. JS round brush
php继承(extends)
[golang syntax] be careful with the copy of slices
Fabric. JS iText sets the color and background color of the specified text
小程序跳装到公众号
运动健身的一些心得经验
php按照指定字符,获取字符串中的部分值,并重组剩余字符串为新的数组
brew install * 失败,解决方法
Vite打包后的dist不能直接在浏览器打开吗
PHP 开发与测试 Webservice(SOAP)-Win
OLED12864 液晶屏