当前位置:网站首页>《剑指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)。
边栏推荐
- Linear table sequence table comprehensive application problem p18
- DS90UB949
- Spl06-007 air pressure sensor (example of barometer)
- 同事写了一个责任链模式,bug无数...
- GCC compilation process and dynamic link library and static link library
- C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
- Kibana - installation and configuration of kibana
- Gut | 香港中文大学于君组揭示吸烟改变肠道菌群并促进结直肠癌(不要吸烟)
- MCDF实验1
- VS2015的下载地址和安装教程
猜你喜欢

Visual Studio 2022下载及配置OpenCV4.5.5

Analysis of EPS electric steering system
![Capturing and sorting out external Fiddler -- Conversation bar and filter [2]](/img/04/e9cc027d753e7049f273d866eefdce.png)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]

Web安全总结

Excel快速跨表复制粘贴

Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.

Spl06-007 air pressure sensor (example of barometer)

GCC compilation process and dynamic link library and static link library

鸿蒙第四次培训
![抓包整理外篇fiddler———— 会话栏与过滤器[二]](/img/04/e9cc027d753e7049f273d866eefdce.png)
抓包整理外篇fiddler———— 会话栏与过滤器[二]
随机推荐
Unity3D学习笔记5——创建子Mesh
简单工厂和工厂方法模式
2022 northeast four provinces match VP record / supplementary questions
Cadence background color setting
多维度监控:智能监控的数据基础
Matlab extracts numerical data from irregular txt files (simple and practical)
Dynamic programming (interval DP)
Multi dimensional monitoring: the data base of intelligent monitoring
PHP基础
软考中级软件设计师该怎么备考
Arctangent entropy: the latest SCI paper in July 2022
外插散点数据
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
DS90UB949
The world's most popular font editor FontCreator tool
asyncio 警告 DeprecationWarning: There is no current event loop
Excel quick cross table copy and paste
Nestjs configuration service, configuring cookies and sessions
程序员的创业陷阱:接私活
Based on MCU, how to realize OTA differential upgrade with zero code and no development?