当前位置:网站首页>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
};
```
边栏推荐
- C how to set two columns comboboxcolumn in DataGridView to bind a secondary linkage effect of cascading events
- numpy 下载安装
- [Yu Yue education] higher mathematics of Nanchang University (2) reference materials
- Why rdd/dataset is needed in spark
- GNN,请你的网络层数再深一点~
- Microsoft technology empowerment position - February course Preview
- GPS从入门到放弃(十五)、DCB差分码偏差
- LeetCode:1189. The maximum number of "balloons" -- simple
- JPEG2000 matlab source code implementation
- GPS from getting started to giving up (19), precise ephemeris (SP3 format)
猜你喜欢

What can one line of code do?
![Leetcode topic [array] -118 Yang Hui triangle](/img/77/d8a7085968cc443260b4c0910bd04b.jpg)
Leetcode topic [array] -118 Yang Hui triangle

Five wars of Chinese Baijiu

The golden age of the U.S. technology industry has ended, and there have been constant lamentations about chip sales and 30000 layoffs

bat脚本学习(一)

Make menuconfig has a recipe for target 'menuconfig' failed error

Microsoft technology empowerment position - February course Preview

爬虫实战(五):爬豆瓣top250

Unity3d Learning Notes 6 - GPU instantiation (1)

GPS从入门到放弃(十二)、 多普勒定速
随机推荐
Realization of epoll reactor model
Broadcast variables and accumulators in spark
Unity3D学习笔记6——GPU实例化(1)
GPS from getting started to giving up (XV), DCB differential code deviation
Search element topic (DFS)
GPS从入门到放弃(十一)、差分GPS
Kohana database
MySQL related terms
Explain ESM module and commonjs module in simple terms
【10点公开课】:视频质量评价基础与实践
Xiaoman network model & http1-http2 & browser cache
GPS从入门到放弃(十二)、 多普勒定速
Qt | UDP广播通信、简单使用案例
Univariate cubic equation - relationship between root and coefficient
LeetCode学习记录(从新手村出发之杀不出新手村)----1
20 large visual screens that are highly praised by the boss, with source code templates!
MySQL - transaction details
十一、服务介绍及端口
Some problems about the use of char[] array assignment through scanf..
Leetcode learning records (starting from the novice village, you can't kill out of the novice Village) ---1