当前位置:网站首页>《剑指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)。
边栏推荐
- 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
- Asyncio warning deprecationwarning: there is no current event loop
- This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
- Repo ~ common commands
- Processes and threads
- 鸿蒙第三次培训(项目实训)
- Using onvif protocol to operate the device
- Programmers' entrepreneurial trap: taking private jobs
- . \vmware-vdiskmanager. exe -k “c:\\xxxxx.vmdk”
- PHP Basics
猜你喜欢
Incremental database backup - DB incr DB full
Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable
Cadence background color setting
Processes and threads
2022 东北四省赛 VP记录/补题
The excel table is transferred to word, and the table does not exceed the edge paper range
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
Xml的(DTD,xml解析,xml建模)
VS2015的下载地址和安装教程
Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
随机推荐
Redis things
C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
C语言 AES加解密
Technical experts from large factories: how can engineers improve their communication skills?
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
MATLAB提取不規則txt文件中的數值數據(簡單且實用)
Phpcms prompt message page Jump to showmessage
Key switch: press FN when pressing F1-F12
STL教程9-容器元素深拷贝和浅拷贝问题
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
Visual Studio 2022下载及配置OpenCV4.5.5
P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap
Dynamic programming (interval DP)
How to get started embedded future development direction of embedded
[OBS] encapsulate the basic process of OBS acquisition
The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
Gut | 香港中文大学于君组揭示吸烟改变肠道菌群并促进结直肠癌(不要吸烟)
MCDF实验1
Machine learning 3.2 decision tree model learning notes (to be supplemented)
R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)