当前位置:网站首页>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;
};
边栏推荐
- [sword finger offer] serialized binary tree
- Tcp/ip protocol
- LeetCode:836. Rectangle overlap
- Revit secondary development Hof method calls transaction
- After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
- After reading the programmer's story, I can't help covering my chest...
- Niuke winter vacation training 6 maze 2
- 优秀的软件测试人员,都具备这些能力
- LeetCode:劍指 Offer 42. 連續子數組的最大和
- R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
猜你喜欢
ROS compilation calls the third-party dynamic library (xxx.so)
Screenshot in win10 system, win+prtsc save location
After reading the programmer's story, I can't help covering my chest...
Navicat premium create MySQL create stored procedure
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
Restful API design specification
可变长参数
swagger设置字段required必填
UML圖記憶技巧
Esp8266-rtos IOT development
随机推荐
LeetCode:41. 缺失的第一个正数
LeetCode:498. Diagonal traversal
LeetCode:39. Combined sum
What are the common processes of software stress testing? Professional software test reports issued by companies to share
LeetCode:836. Rectangle overlap
LeetCode:236. The nearest common ancestor of binary tree
如何进行接口测试测?有哪些注意事项?保姆级解读
hutool优雅解析URL链接并获取参数
深度剖析C语言指针
Sublime text using ctrl+b to run another program without closing other runs
LeetCode:673. Number of longest increasing subsequences
Visual implementation and inspection of visdom
LeetCode:221. Largest Square
Li Kou daily question 1 (2)
To effectively improve the quality of software products, find a third-party software evaluation organization
Variable length parameter
LeetCode:剑指 Offer 03. 数组中重复的数字
swagger设置字段required必填
角色动画(Character Animation)的现状与趋势
pytorch查看张量占用内存大小