当前位置:网站首页>500. Keyboard line
500. Keyboard line
2022-07-02 06:01:00 【occasionally.】
1. Title Description
Here's an array of strings words , Returns only those that can be used in American keyboard Words printed with letters on the same line . The keyboard is shown in the figure below .
American keyboard in :
The first line consists of the characters “qwertyuiop” form .
The second line consists of characters “asdfghjkl” form .
The third line consists of the characters “zxcvbnm” form .
Example :
Input :words = [“Hello”,“Alaska”,“Dad”,“Peace”]
Output :[“Alaska”,“Dad”]
Input :words = [“adsdf”,“sfd”]
Output :[“adsdf”,“sfd”]
2. Their thinking
For the input words , Convert it to lowercase first , Then judge whether each letter of the word is a subset of a certain line .
- stay python Through aggregate To quickly determine whether the letters that make up the word are a subset of a line
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))
- stay C++ Judge whether each letter of the word is in a line in turn
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. Knowledge review
- python– aggregate
- python–filter function
边栏推荐
- Fundamentals of software testing
- Eco express micro engine system has supported one click deployment to cloud hosting
- php读文件(读取文件内含有某字符串的指定行)
- [whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP
- ROS2----LifecycleNode生命周期节点总结
- "Simple" infinite magic cube
- 《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
- Introduce uview into uni app
- Redis Key-Value数据库 【高级】
- Nacos 启动报错 Error creating bean with name ‘instanceOperatorClientImpl‘ defined in URL
猜你喜欢

让每一位开发者皆可使用机器学习技术

CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现

51单片机——ADC讲解(A/D转换、D/A转换)
![Redis key value database [primary]](/img/47/10461d12720a9dd801f80ed1d3ad23.jpg)
Redis key value database [primary]

Fundamentals of software testing

深度学习分类网络 -- AlexNet

3D printer G code command: complete list and tutorial

Shenji Bailian 3.54-dichotomy of dyeing judgment

"Simple" infinite magic cube

Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
随机推荐
Grbl software: basic knowledge of simple explanation
【論文翻譯】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
STC8H8K系列汇编和C51实战——数码管显示ADC、按键串口回复按键号与ADC数值
Software testing Q & A
数理统计与机器学习
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
死磕大屏UI,FineReport开发日记
Lambda 表达式 和 方法引用
经典文献阅读之--Deformable DETR
PHP extensions
Problems encountered in uni app development (continuous update)
Mock simulate the background return data with mockjs
Several keywords in C language
OLED12864 液晶屏
经典文献阅读之--SuMa++
STC8H8K系列汇编和C51实战——串口发送菜单界面选择不同功能
Lambda expressions and method references
Use some common functions of hbuilderx
[C language] screening method for prime numbers