当前位置:网站首页>Finding JS in the two-dimensional array of sword fingers (clear version)
Finding JS in the two-dimensional array of sword fingers (clear version)
2022-06-09 04:58:00 【Madrid Tianxin】
In a n * m In a two-dimensional array , Each row is sorted in ascending order from left to right , Each column is sorted in ascending order from top to bottom . Please complete an efficient function , Enter such a two-dimensional array and an integer , Determine whether the array contains the integer .
Example :
The existing matrix matrix(5) as follows :
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false.
My solution :
Say to violent problem solving no!!!!!!!!!!!!!! Traversal one by one takes up too much memory , And it is not the intention of this question .
The number of arrays is matrix.length, And the row is from the index 0 The line , So you have to subtract 1
First, let's start with the element in the lower left corner ----- That's ok =matrix.length-1 Column =0
If the target value > This value , Then move to the right , That is, column +1; If the target value < This value , Then move up , That is to say, yes -1.
If the target value = This value , Then return to true; If the array length is 0 Words , Indicates that there is no value , Then return to false;
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var findNumberIn2DArray = function (matrix, target) {
let i = matrix.length - 1 // The value of the lower left row
let j = 0; // The first column value is 0
if(matrix.length===0)return false
while (i >= 0 && j <matrix[0].length) {
if (target > matrix[i][j]) {
j++;
}
else if (target < matrix[i][j]) {
i--;
}
else if (target === matrix[i][j]) {
return true
}
}
return false
};边栏推荐
- Question bank and answers of G3 boiler water treatment examination in 2022
- [django learning notes - 12]: database operation
- TypeScript 学习【8】枚举类型
- MySQL scheduled backup restore
- P1779 Xiaohu's springboard
- 2022年G3锅炉水处理考试题库及答案
- Make in-depth research and summary, go to a higher level, and make new year's resolutions
- [006] [esp32 Development Notes] steps for burning firmware using flash download tool
- wps ppt背景图片如何换颜色
- ^26浏览器的内核
猜你喜欢

API 網關 Apache APISIX 在 AWS Graviton3 上的安裝與性能測試

Mmdet modify the font size, position, color and fill box of the detection box

"Diwen Cup" skill competition between teachers and students of Electrical Engineering Department of Zibo technician college was successfully held

ASP. Net core build scheduling service - use generic host with quartz Net
![[004] [esp32 Development Notes] audio development framework ADF environment construction - based on esp-idf](/img/55/9eb286bc56ec991837fc014b42fc20.png)
[004] [esp32 Development Notes] audio development framework ADF environment construction - based on esp-idf

openGL_01-創建窗口

Lighting - brightness attenuation of light

OpenGL 01 - créer une fenêtre

openGL_ 04 drawing with index array

2022-06-清华管理学-清华大学-宁向东
随机推荐
“迪文杯”湖南文理学院电子设计大赛圆满结束
Win10注册表无法保存对权限所作的更改拒绝访问
2022年R2移动式压力容器充装考试模拟100题及模拟考试
2022焊工(初级)特种作业证考试题库及模拟考试
SWFUpload
Penetration test path dictionary, blasting dictionary
TypeScript 学习【9】泛型
[004] [ESP32开发笔记] 音频开发框架ADF环境搭建——基于ESP-IDF
[006] [esp32 Development notes] burn firmware steps Using Flash Download Tool
[C language] all the experts are double cultivators. At the grammatical level, they are almost plain and plain. They never show off their skills. They show off their ideas!
Program implementation of inserting, updating and deleting in Oracle Internet cafe design
Mmdet modify the font size, position, color and fill box of the detection box
Lighting - 光的亮度衰减
TypeScript学习【5】 类类型
Cloud computing technology
Latest list of 955 companies that do not work overtime (2022 Edition)
聊聊保证线程安全的10个小技巧
Installation and performance test of API gateway Apache APIs IX on AWS graviton3
Ultimate shell - Zsh
Recommend this UI automation testing framework and write use cases as colloquially as possible