当前位置:网站首页>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;
};
边栏推荐
- Unsupported operation exception
- 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
- Indentation of tabs and spaces when writing programs for sublime text
- Problems in loading and saving pytorch trained models
- 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
- swagger设置字段required必填
- Navicat Premium 创建MySql 创建存储过程
- LeetCode:剑指 Offer 04. 二维数组中的查找
- 可变长参数
- LeetCode:673. Number of longest increasing subsequences
猜你喜欢
C语言深度解剖——C语言关键字
Charging interface docking tutorial of enterprise and micro service provider platform
Detailed explanation of dynamic planning
Export IEEE document format using latex
Detailed explanation of heap sorting
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
Navicat Premium 创建MySql 创建存储过程
Esp8266-rtos IOT development
C语言双指针——经典题型
随机推荐
JVM quick start
After reading the programmer's story, I can't help covering my chest...
Compétences en mémoire des graphiques UML
What are the common processes of software stress testing? Professional software test reports issued by companies to share
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
【嵌入式】Cortex M4F DSP库
Nacos 的安装与服务的注册
pytorch查看张量占用内存大小
Swagger setting field required is mandatory
Crash problem of Chrome browser
ROS compilation calls the third-party dynamic library (xxx.so)
LeetCode:剑指 Offer 03. 数组中重复的数字
TP-LINK enterprise router PPTP configuration
[MySQL] limit implements paging
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
[OC-Foundation框架]---【集合数组】
Notes 01
UnsupportedOperationException异常
Problems encountered in connecting the database of the project and their solutions