当前位置:网站首页>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;
};
边栏推荐
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- Marathon envs project environment configuration (strengthen learning and imitate reference actions)
- 优秀的软件测试人员,都具备这些能力
- 深度剖析C语言数据在内存中的存储
- C语言双指针——经典题型
- [OC]-<UI入门>--常用控件-UIButton
- Leetcode: Jianzhi offer 03 Duplicate numbers in array
- marathon-envs项目环境配置(强化学习模仿参考动作)
- Tcp/ip protocol
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
猜你喜欢

LeetCode:124. 二叉树中的最大路径和

目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台

Warning in install. packages : package ‘RGtk2’ is not available for this version of R

深度剖析C语言指针
![[embedded] print log using JLINK RTT](/img/22/c37f6e0f3fb76bab48a9a5a3bb3fe5.png)
[embedded] print log using JLINK RTT

Navicat Premium 创建MySql 创建存储过程

Tcp/ip protocol

深度剖析C语言数据在内存中的存储

角色动画(Character Animation)的现状与趋势

UML图记忆技巧
随机推荐
MYSQL卸载方法与安装方法
Revit 二次开发 HOF 方式调用transaction
使用latex导出IEEE文献格式
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
POI add write excel file
[MySQL] multi table query
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
LeetCode:498. Diagonal traversal
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:673. Number of longest increasing subsequences
Bitwise logical operator
Hutool gracefully parses URL links and obtains parameters
LeetCode:673. 最长递增子序列的个数
MongoDB 的安装和基本操作
力扣每日一题(二)
View computer devices in LAN
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
Light of domestic games destroyed by cracking
LeetCode:剑指 Offer 04. 二维数组中的查找
Excellent software testers have these abilities