当前位置:网站首页>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
};
```
边栏推荐
- 小满网络模型&http1-http2 &浏览器缓存
- 14 years Bachelor degree, transferred to software testing, salary 13.5k
- 微信红包封面小程序源码-后台独立版-带测评积分功能源码
- Qt | UDP广播通信、简单使用案例
- Kohana 数据库
- PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
- 关于程序员的职业操守,从《匠艺整洁之道》谈起
- C how to set two columns comboboxcolumn in DataGridView to bind a secondary linkage effect of cascading events
- MariaDb数据库管理系统的学习(一)安装示意图
- GPS从入门到放弃(十九)、精密星历(sp3格式)
猜你喜欢
GPS从入门到放弃(十六)、卫星时钟误差和卫星星历误差
基于LM317的可调直流电源
GNN,请你的网络层数再深一点~
爬虫实战(五):爬豆瓣top250
C how to set two columns comboboxcolumn in DataGridView to bind a secondary linkage effect of cascading events
make menuconfig出现recipe for target ‘menuconfig‘ failed错误
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
AI 企业多云存储架构实践 | 深势科技分享
Make menuconfig has a recipe for target 'menuconfig' failed error
GNN, please deepen your network layer~
随机推荐
Make menuconfig has a recipe for target 'menuconfig' failed error
Shell product written examination related
Record the process of cleaning up mining viruses
Persistence / caching of RDD in spark
Depth first traversal (DFS) and breadth first traversal (BFS)
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
Guava: use of multiset
JPEG2000-Matlab源码实现
Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
[MySQL] online DDL details
Write a rotation verification code annotation gadget with aardio
Mysql相关术语
uni-app App端半屏连续扫码
GNN, please deepen your network layer~
Explain ESM module and commonjs module in simple terms
mysql根据两个字段去重
Solution to the problem of UOS boot prompt unlocking login password ring
AI 企业多云存储架构实践 | 深势科技分享
50 commonly used numpy function explanations, parameters and usage examples
The relationship between root and coefficient of quadratic equation with one variable