当前位置:网站首页>《剑指offer 04》二维数组查找
《剑指offer 04》二维数组查找
2022-07-03 11:02:00 【爱敲代码的小邢~】
《剑指offer 04》二维数组查找
【LeetCode链接】
【题目】
在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
示例:
[
[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
【思路】🧐
首先想到的思路就是遍历数组,找到就返回true,找不到就返回false,这样的时间复杂度为O(N*M),那有没有更优的方法呢?
由题得,每一行都是递增的,每一列也都是递增的,所以我们会萌生二分搜索的思想。
那怎么在二维数组中运用二分的思想呢?
我们知道,在一位数组中二分的核心就是找到中间下标mid,在二维数组中,这个mid就是每两行中前一行的最后一个元素的下标。
为什么呢?
因为我们是否在本行查找或是到下一行查找就取决于target与改下标对于元素值的大小关系,若target小于该元素,则我们在本行找;若target大于该元素,则我们到下面的行去找。
【步骤】

这里要注意 matrix.size() 是行数,matrix[0].size() 是列数。
【代码】
class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
if (matrix.empty() == true)//如果矩阵为空,则一定没有target
return false;
int i = 0;
int j = matrix[0].size()-1;
while (i<matrix.size() && j >= 0)
{
if (matrix[i][j] == target){
return true;
}
if (matrix[i][j]>target){
j--;
}
else{
i++;
}
}
return false;
}
};
【时间复杂度】
该算法最坏情况就是走到左下位置,故时间复杂度为O(N+M)。
边栏推荐
- Redis things
- 基于turtlebot3实现SLAM建图及自主导航仿真
- Incremental database backup - DB incr DB full
- Nestjs configuration service, configuring cookies and sessions
- Hongmeng fourth training
- Programmers' entrepreneurial trap: taking private jobs
- 程序员的创业陷阱:接私活
- ORACLE进阶(一) 通过EXPDP IMPDP命令实现导dmp
- Excel快速跨表复制粘贴
- Based on MCU, how to realize OTA differential upgrade with zero code and no development?
猜你喜欢

ArcGIS应用(二十一)Arcmap删除图层指定要素的方法

Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable

外插散点数据

After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
![抓包整理外篇fiddler———— 会话栏与过滤器[二]](/img/04/e9cc027d753e7049f273d866eefdce.png)
抓包整理外篇fiddler———— 会话栏与过滤器[二]

Visual Studio 2022下载及配置OpenCV4.5.5

MATLAB extrait les données numériques d'un fichier txt irrégulier (simple et pratique)

Extrapolated scatter data

AOSP ~ NTP ( 网络时间协议 )

【学习笔记】dp 状态与转移
随机推荐
Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
MySQL union和union all区别
Machine learning 3.2 decision tree model learning notes (to be supplemented)
Leetcode 46: full arrangement
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
How should intermediate software designers prepare for the soft test
解决msvcp120d.dll和msvcr120d.dll缺失
836. 合并集合(DAY 63)并查集
[OBS] encapsulate the basic process of OBS acquisition
Qt+VTK+OCCT读取IGES/STEP模型
Using onvif protocol to operate the device
typeScript
Application of high-precision indoor positioning technology in safety management of smart factory
How to make others fear you
Understand go language context in one article
Incremental database backup - DB incr DB full
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
优化接口性能
.\vmware-vdiskmanager.exe -k “c:\\xxxxx.vmdk”
asyncio 警告 DeprecationWarning: There is no current event loop