当前位置:网站首页>Force deduction question 500, keyboard line, JS implementation
Force deduction question 500, keyboard line, JS implementation
2022-07-06 22:04:00 【Big chicken legs are best】
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 .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/keyboard-row
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Example 1:
Input :words = ["Hello","Alaska","Dad","Peace"]
Output :["Alaska","Dad"]
Example 2:
Input :words = ["omk"]
Output :[]
Example 3:
Input :words = ["adsdf","sfd"]
Output :["adsdf","sfd"]
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/keyboard-row
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
### Their thinking
Here is the solution
On the keyboard 3 Rows correspond to each other 3 An array
2 Secondary cycle , Take out each word for the first time, turn them into lowercase and then into array
The first word corresponds to which array it belongs
Start the second cycle if there is a letter of the word that does not belong to the array
Just exit the loop , Don't add the word to the array
### Code
```javascript
/**
* @param {string[]} words
* @return {string[]}
*/
var findWords = function(words) {
let a=['q','w','e','r','t','y','u','i','o','p']
let b=['a','s','d','f','g','h','j','k','l']
let c=['z','x','c','v','b','n','m'],flag=false,k=[]
for(let i=0;i<words.length;i++){
let n=words[i].toLowerCase().split('')
if(a.includes(n[0])){
for(let j=1;j<n.length;j++){
if(!a.includes(n[j])){
flag=true
break
}
}
}else if(b.includes(n[0])){
for(let j=1;j<n.length;j++){
if(!b.includes(n[j]))
{
flag=true
break
}
}
}else{
for(let j=1;j<n.length;j++){
if(!c.includes(n[j]))
{flag=true
break}
}
}
if(!flag)k.push(words[i])
flag=false
}
return k
};
```
边栏推荐
- Broadcast variables and accumulators in spark
- From campus to Tencent work for a year of those stumbles!
- UNI-Admin基础框架怎么关闭创建超级管理员入口?
- PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
- Sparkshuffle process and Mr shuffle process
- Is it important to build the SEO foundation of the new website
- Leetcode learning records (starting from the novice village, you can't kill out of the novice Village) ---1
- Reinforcement learning - learning notes 5 | alphago
- Earned value management EVM detailed explanation and application, example explanation
- Oracle性能分析3:TKPROF简介
猜你喜欢
Why rdd/dataset is needed in spark
Checkpoint of RDD in spark
Enhance network security of kubernetes with cilium
LeetCode:1189. The maximum number of "balloons" -- simple
Adjustable DC power supply based on LM317
Search element topic (DFS)
Earned value management EVM detailed explanation and application, example explanation
[Yu Yue education] higher mathematics of Nanchang University (2) reference materials
Shake Sound poussera l'application indépendante de plantation d'herbe "louable", les octets ne peuvent pas oublier le petit livre rouge?
GPS从入门到放弃(十七) 、对流层延时
随机推荐
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
Problems in the process of opencv300 cmake generating project
GPS from getting started to giving up (XVIII), multipath effect
Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
Record the process of cleaning up mining viruses
make menuconfig出现recipe for target ‘menuconfig‘ failed错误
Guava: three ways to create immutablexxx objects
数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
MariaDB database management system learning (I) installation diagram
ViT论文详解
【MySQL】Online DDL详解
Univariate cubic equation - relationship between root and coefficient
MongoDB(三)——CRUD
用aardio写一个旋转验证码标注小工具
What is the difference between animators and animators- What is the difference between an Animator and an Animation?
Checkpoint of RDD in spark
How does the uni admin basic framework close the creation of super administrator entries?
Oracle Performance Analysis 3: introduction to tkprof
Five wars of Chinese Baijiu
Sql: stored procedures and triggers - Notes