当前位置:网站首页>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;
};
边栏推荐
- pytorch查看张量占用内存大小
- Indentation of tabs and spaces when writing programs for sublime text
- UML diagram memory skills
- LeetCode:394. String decoding
- 企微服务商平台收费接口对接教程
- Philosophical enlightenment from single point to distributed
- Tcp/ip protocol
- LeetCode:124. 二叉树中的最大路径和
- @JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
- Deep anatomy of C language -- C language keywords
猜你喜欢
使用latex导出IEEE文献格式
优秀的软件测试人员,都具备这些能力
Simple use of promise in uniapp
Roguelike game into crack the hardest hit areas, how to break the bureau?
Visual implementation and inspection of visdom
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
JVM quick start
[embedded] print log using JLINK RTT
MYSQL卸载方法与安装方法
同一局域网的手机和电脑相互访问,IIS设置
随机推荐
随手记01
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
使用latex导出IEEE文献格式
C語言雙指針——經典題型
LeetCode:221. 最大正方形
Variable length parameter
Detailed explanation of heap sorting
LeetCode:394. 字符串解码
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)
Esp8266-rtos IOT development
LeetCode:214. Shortest palindrome string
UML diagram memory skills
The harm of game unpacking and the importance of resource encryption
同一局域网的手机和电脑相互访问,IIS设置
Image, CV2 read the conversion and size resize change of numpy array of pictures
[NVIDIA development board] FAQ (updated from time to time)
Leetcode: Sword finger offer 48 The longest substring without repeated characters
Cesium draw points, lines, and faces
LeetCode:41. 缺失的第一个正数
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)