当前位置:网站首页>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;
};
边栏推荐
- Cesium draw points, lines, and faces
- Delay initialization and sealing classes
- TP-LINK 企业路由器 PPTP 配置
- 随手记01
- View computer devices in LAN
- 超高效!Swagger-Yapi的秘密
- Screenshot in win10 system, win+prtsc save location
- 角色动画(Character Animation)的现状与趋势
- Problems encountered in connecting the database of the project and their solutions
- [embedded] cortex m4f DSP Library
猜你喜欢

C language double pointer -- classic question type

TP-LINK enterprise router PPTP configuration

Charging interface docking tutorial of enterprise and micro service provider platform

JVM quick start

数学建模2004B题(输电问题)

704 binary search

Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges

Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)

Double pointeur en langage C - - modèle classique

Alibaba cloud server mining virus solution (practiced)
随机推荐
Screenshot in win10 system, win+prtsc save location
LeetCode:26. 删除有序数组中的重复项
Image, CV2 read the conversion and size resize change of numpy array of pictures
Sublime text using ctrl+b to run another program without closing other runs
JVM quick start
LeetCode:41. Missing first positive number
MongoDB 的安装和基本操作
LeetCode:498. 对角线遍历
【剑指offer】序列化二叉树
[sword finger offer] serialized binary tree
企微服务商平台收费接口对接教程
Double pointeur en langage C - - modèle classique
Philosophical enlightenment from single point to distributed
Compétences en mémoire des graphiques UML
Promise 在uniapp的简单使用
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
704 binary search
LeetCode:836. 矩形重叠
[OC]-<UI入门>--常用控件-UIButton
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)