当前位置:网站首页>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
边栏推荐
- PHP obtains some values in the string according to the specified characters, and reorganizes the remaining strings into a new array
- Vscode paste image plugin saves image path settings
- php获取cpu使用率、硬盘使用、内存使用
- Stick to the big screen UI, finereport development diary
- php数组转化为xml
- File contains vulnerability (I)
- Fundamentals of software testing
- Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
- 【論文翻譯】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
- Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
猜你喜欢
经典文献阅读之--Deformable DETR
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
从设计交付到开发,轻松畅快高效率!
深度学习分类网络 -- AlexNet
Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
如何使用MITMPROXy
Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
让每一位开发者皆可使用机器学习技术
CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
测试 - 用例篇
随机推荐
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
Redis key value database [primary]
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
从设计交付到开发,轻松畅快高效率!
Shenji Bailian 3.53-kruskal
JWT tool class
Practice C language advanced address book design
Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
Redis Key-Value数据库 【秒杀】
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
Lambda expressions and method references
Regular expression summary
复杂 json数据 js前台解析 详细步骤《案例:一》
492.构造矩形
图片裁剪插件cropper.js
[C language] simple implementation of mine sweeping game
Detailed notes of ES6
CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
MUI底部导航的样式修改
Redis Key-Value数据库 【高级】