当前位置:网站首页>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
};
```
边栏推荐
- 50 commonly used numpy function explanations, parameters and usage examples
- 数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
- 1292_FreeROS中vTaskResume()以及xTaskResumeFromISR()的实现分析
- PostgreSQL install GIS plug-in create extension PostGIS_ topology
- 20 large visual screens that are highly praised by the boss, with source code templates!
- C# 如何在dataGridView里设置两个列comboboxcolumn绑定级联事件的一个二级联动效果
- Reptile practice (V): climbing watercress top250
- 【10点公开课】:视频质量评价基础与实践
- Some problems about the use of char[] array assignment through scanf..
- GPS从入门到放弃(十一)、差分GPS
猜你喜欢
Search element topic (DFS)
GPS从入门到放弃(十三)、接收机自主完好性监测(RAIM)
PostgreSQL install GIS plug-in create extension PostGIS_ topology
Leetcode topic [array] -118 Yang Hui triangle
Sparkshuffle process and Mr shuffle process
Vit paper details
GPS从入门到放弃(十四)、电离层延时
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
GPS从入门到放弃(十六)、卫星时钟误差和卫星星历误差
ViT论文详解
随机推荐
Kohana database
GPS从入门到放弃(十八)、多路径效应
Numpy download and installation
Persistence / caching of RDD in spark
Run the deep network on PI and Jetson nano, and the program is killed
Shell product written examination related
mysql根据两个字段去重
1292_ Implementation analysis of vtask resume() and xtask resume fromisr() in freeros
Xiaoman network model & http1-http2 & browser cache
GPS從入門到放弃(十三)、接收機自主完好性監測(RAIM)
抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
Aggregate function with key in spark
ViT论文详解
Microsoft technology empowerment position - February course Preview
GPS from getting started to giving up (XX), antenna offset
20 large visual screens that are highly praised by the boss, with source code templates!
搜素专题(DFS )
GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
Method return value considerations
[go][reprint]vscode run a HelloWorld example after configuring go