当前位置:网站首页>Leetcode: Jianzhi offer 04 Search in two-dimensional array
Leetcode: Jianzhi offer 04 Search in two-dimensional array
2022-07-06 08:51:00 【Bertil】
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 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.
Limit :
0 <= n <= 1000
0 <= m <= 1000
Be careful : This topic and the main station 240 The question is the same :https://leetcode-cn.com/problems/search-a-2d-matrix-ii/
Their thinking
1. First, define the coordinates of the lower left corner ,
2. Then traverse the matrix from the lower left element : If the current element > target, Then move up one line ; If the current element < target, Move one column to the right ; If the current element = target, Then return directly true that will do
3. Finally, after searching through the above loop , If not found equal to target The element of returns directly false that will do
Code
/** * @param {number[][]} matrix * @param {number} target * @return {boolean} */
var findNumberIn2DArray = function(matrix, target) {
if(!matrix.length) return false;
// Define the coordinates of the lower left corner
let [row, col] = [matrix.length - 1, 0];
const len = matrix[0].length;
// The coordinates are in the matrix , Just keep looking for
while (row >= 0 && col <= len - 1) {
// item Represents the current element
const item = matrix[row][col];
if (item === target) {
// find , return true
return true;
} else if (item > target) {
// It's too big , Move up a line
row--;
} else {
// Is too small , Move a column to the right
col++;
}
}
return false;
};
边栏推荐
- Niuke winter vacation training 6 maze 2
- TP-LINK 企业路由器 PPTP 配置
- C語言雙指針——經典題型
- Marathon envs project environment configuration (strengthen learning and imitate reference actions)
- To effectively improve the quality of software products, find a third-party software evaluation organization
- 深度剖析C语言指针
- Revit secondary development Hof method calls transaction
- marathon-envs项目环境配置(强化学习模仿参考动作)
- Crash problem of Chrome browser
- Nacos 的安装与服务的注册
猜你喜欢
随机推荐
Cesium draw points, lines, and faces
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
Crash problem of Chrome browser
Revit secondary development Hof method calls transaction
深度剖析C语言数据在内存中的存储
LeetCode:39. Combined sum
C language double pointer -- classic question type
LeetCode:剑指 Offer 03. 数组中重复的数字
LeetCode:836. Rectangle overlap
Navicat Premium 创建MySql 创建存储过程
项目连接数据库遇到的问题及解决
如何进行接口测试测?有哪些注意事项?保姆级解读
ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
C語言雙指針——經典題型
Screenshot in win10 system, win+prtsc save location
Visual implementation and inspection of visdom
C语言深度解剖——C语言关键字
Simple use of promise in uniapp
CSP first week of question brushing
LeetCode:221. 最大正方形