当前位置:网站首页>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;
};
边栏推荐
- LeetCode:221. Largest Square
- JVM quick start
- 使用latex导出IEEE文献格式
- 如何正确截取字符串(例:应用报错信息截取入库操作)
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- LeetCode:劍指 Offer 42. 連續子數組的最大和
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- Deep analysis of C language data storage in memory
- [OC]-<UI入门>--常用控件的学习
- Marathon envs project environment configuration (strengthen learning and imitate reference actions)
猜你喜欢
同一局域网的手机和电脑相互访问,IIS设置
ESP8266-RTOS物联网开发
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
Delay initialization and sealing classes
Alibaba cloud server mining virus solution (practiced)
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
企微服务商平台收费接口对接教程
优秀的软件测试人员,都具备这些能力
Current situation and trend of character animation
Detailed explanation of dynamic planning
随机推荐
LeetCode:214. Shortest palindrome string
Restful API design specification
Export IEEE document format using latex
LeetCode:162. 寻找峰值
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
LeetCode:387. The first unique character in the string
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
LeetCode:236. 二叉树的最近公共祖先
优秀的软件测试人员,都具备这些能力
LeetCode:836. 矩形重叠
LeetCode:劍指 Offer 42. 連續子數組的最大和
Leetcode: Sword finger offer 48 The longest substring without repeated characters
Tdengine biweekly selection of community issues | phase III
Problems in loading and saving pytorch trained models
Problems encountered in connecting the database of the project and their solutions
The mysqlbinlog command uses
LeetCode:39. 组合总和
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
sublime text中conda环境中plt.show无法弹出显示图片的问题
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串