当前位置:网站首页>LeetCode:剑指 Offer 04. 二维数组中的查找
LeetCode:剑指 Offer 04. 二维数组中的查找
2022-07-06 08:44:00 【Bertil】
在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
示例:
现有矩阵 matrix 如下:
[
[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]
]
给定 target = 5,返回 true。
给定 target = 20,返回 false。
限制:
0 <= n <= 1000
0 <= m <= 1000
注意:本题与主站 240 题相同:https://leetcode-cn.com/problems/search-a-2d-matrix-ii/
解题思路
1.首先定义出左下角的坐标,
2.然后从左下角元素开始遍历矩阵:若当前元素 > target,则上移一行;若当前元素 < target,则右移一列;若当前元素 = target,则直接返回true即可
3.最后通过上述循环遍历查找后,若未找到等于target的元素则直接返回false即可
代码
/** * @param {number[][]} matrix * @param {number} target * @return {boolean} */
var findNumberIn2DArray = function(matrix, target) {
if(!matrix.length) return false;
// 定义左下角的坐标
let [row, col] = [matrix.length - 1, 0];
const len = matrix[0].length;
// 坐标在矩阵内,就一直查找
while (row >= 0 && col <= len - 1) {
// item 表示当前元素
const item = matrix[row][col];
if (item === target) {
// 找到,返回true
return true;
} else if (item > target) {
// 太大了,上移一行
row--;
} else {
// 太小了,右移一列
col++;
}
}
return false;
};
边栏推荐
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- 704 二分查找
- 有效提高软件产品质量,就找第三方软件测评机构
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
- marathon-envs项目环境配置(强化学习模仿参考动作)
- Bitwise logical operator
- Detailed explanation of heap sorting
- Visual implementation and inspection of visdom
- Charging interface docking tutorial of enterprise and micro service provider platform
- Problems in loading and saving pytorch trained models
猜你喜欢
Deep learning: derivation of shallow neural networks and deep neural networks
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
C language double pointer -- classic question type
visdom可视化实现与检查介绍
Light of domestic games destroyed by cracking
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
tree树的精准查询
The harm of game unpacking and the importance of resource encryption
Bottom up - physical layer
sublime text中conda环境中plt.show无法弹出显示图片的问题
随机推荐
gcc动态库fPIC和fpic编译选项差异介绍
JVM 快速入门
Process of obtaining the electronic version of academic qualifications of xuexin.com
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
ESP8266-RTOS物联网开发
View computer devices in LAN
Deep analysis of C language data storage in memory
查看局域网中电脑设备
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
Leetcode question brushing (5.31) string
Bitwise logical operator
MySQL learning record 10getting started with JDBC
torch建立的网络模型使用torchviz显示
Excellent software testers have these abilities
【嵌入式】使用JLINK RTT打印log
ROS compilation calls the third-party dynamic library (xxx.so)
Purpose of computer F1-F12
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
[MySQL] lock
China polyether amine Market Forecast and investment strategy report (2022 Edition)