当前位置:网站首页>"Jianzhi offer 04" two-dimensional array search
"Jianzhi offer 04" two-dimensional array search
2022-07-03 11:49:00 【Xiao Xing who likes to knock code~】
《 The finger of the sword offer 04》 Two dimensional array search
【LeetCode link 】
【 subject 】
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 :
[
[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
【 Ideas 】🧐
The first thought is to traverse the array , Find and return true, Go back if you can't find it false, This time complexity is O(N*M), Is there a better way ?
From the question , Each line is incremented , Each column is also incremented , So we will have the idea of binary search .
How to use the idea of bisection in two-dimensional arrays ?
We know , The core of bisection in a one bit array is to find the middle subscript mid, In a two-dimensional array , This mid Is the subscript of the last element of the previous line in every two lines .
Why? ?
Because whether we search on this line or the next line depends on target And the size of the subscript for the element value , if target Less than this element , Then we are looking for ; if target Greater than this element , Then let's go to the following line to find .
【 step 】

Pay attention here matrix.size() It's the number of lines ,matrix[0].size() It's the number of columns .
【 Code 】
class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
if (matrix.empty() == true)// If the matrix is empty , Then there must be no 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;
}
};
【 Time complexity 】
The worst case of this algorithm is to go to the lower left , So the time complexity is O(N+M).
边栏推荐
- Repo ~ common commands
- Kibana~Kibana的安装和配置
- 利用Zabbix动态监控磁盘I/O
- Mmc5603nj geomagnetic sensor (Compass example)
- rxjs Observable filter Operator 的实现原理介绍
- FL Studio 20 unlimited trial fruit arranger Download
- 软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。
- DS90UB949
- Linear table sequence table comprehensive application problem p18
- R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
猜你喜欢

The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.

2022 northeast four provinces match VP record / supplementary questions

This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system

After using the thread pool for so long, do you really know how to reasonably configure the number of threads?

vulnhub之GeminiInc v2

量化计算调研

鸿蒙第三次培训(项目实训)

Slam mapping and autonomous navigation simulation based on turnlebot3

银泰百货点燃城市“夜经济”

Modular programming of single chip microcomputer
随机推荐
PHP server interacts with redis with a large number of close_ Wait analysis
Viewing binary bin files with notepad++ editor
Excel quick cross table copy and paste
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
简单工厂和工厂方法模式
Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable
动态规划(区间dp)
MySQL union和union all区别
Concurrent programming - singleton
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
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
The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
(数据库提权——Redis)Redis未授权访问漏洞总结
STL Tutorial 9 deep copy and shallow copy of container elements
P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap
Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
AOSP ~ NTP ( 网络时间协议 )
Yintai department store ignites the city's "night economy"
一些常用术语