当前位置:网站首页>"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).
边栏推荐
- 导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
- 金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
- phpcms 提示信息頁面跳轉showmessage
- 《剑指offer 04》二维数组查找
- 抓包整理外篇fiddler———— 会话栏与过滤器[二]
- R language uses grid of gridextra package The array function combines multiple visual images of the ggplot2 package horizontally, and the ncol parameter defines the number of columns of the combined g
- 2022年湖南工学院ACM集训第二次周测题解
- vulnhub之cereal
- P3250 [HNOI2016] 网络 + [NECPC2022] F.Tree Path 树剖+线段树维护堆
- C language AES encryption and decryption
猜你喜欢

Cadence background color setting

Ripper of vulnhub

vulnhub之pyexp

Web安全总结

2022 northeast four provinces match VP record / supplementary questions

After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good

vulnhub之momentum

836. Merge sets (day 63) and search sets

Arctangent entropy: the latest SCI paper in July 2022

Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
随机推荐
Repo ~ common commands
《剑指offer 04》二维数组查找
Xml的(DTD,xml解析,xml建模)
The excel table is transferred to word, and the table does not exceed the edge paper range
Some common terms
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
Understand go language context in one article
Phpcms prompt message page Jump showmessage
DS90UB949
【学习笔记】dp 状态与转移
Ripper of vulnhub
R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA
鸿蒙第三次培训(项目实训)
导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
Cacti监控Redis实现过程
Linear table sequence table comprehensive application problem p18
Viewing binary bin files with notepad++ editor
MySQL searches and sorts out common methods according to time
PHP基础
《剑指offer 03》数组中重复的数字