当前位置:网站首页>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
边栏推荐
- Vscode paste image plugin saves image path settings
- mysql的约束总结
- 如何使用MITMPROXy
- Use some common functions of hbuilderx
- Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
- 正则表达式总结
- Redis key value database [advanced]
- cookie插件和localForage离线储存插件
- Memcached installation
- 文件包含漏洞(二)
猜你喜欢

Oled12864 LCD screen

ES6的详细注解

ROS2----LifecycleNode生命周期节点总结

Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles

Fundamentals of software testing

Software testing Q & A

借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力

【论文翻译】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
![[paper translation] gcnet: non local networks meet squeeze exception networks and beyond](/img/7a/718162d08796f70251511101b3a61b.png)
[paper translation] gcnet: non local networks meet squeeze exception networks and beyond

Comment utiliser mitmproxy
随机推荐
Memcached installation
Lambda 表达式 和 方法引用
Reading notes of cgnf: conditional graph neural fields
Detailed notes of ES6
【LeetCode】Day92-盛最多水的容器
Eco express micro engine system has supported one click deployment to cloud hosting
How to write good code - Defensive Programming Guide
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
我所理解的DRM显示框架
ESP8266与STC8H8K单片机联动——天气时钟
脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四
memcached安装
php按照指定字符,获取字符串中的部分值,并重组剩余字符串为新的数组
STC8H8K系列汇编和C51实战——按键允许按键计数(利用下降沿中断控制)
3D printer G code command: complete list and tutorial
正则表达式总结
Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
TypeScript的泛型和泛型约束
Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
外部中断无法进入,删代码再还原就好......记录这个想不到的bug