当前位置:网站首页>"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).
边栏推荐
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- vulnhub之Ripper
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
- Dynamic programming (interval DP)
- Nestjs配置服务,配置Cookie和Session
- .\vmware-vdiskmanager.exe -k “c:\\xxxxx.vmdk”
- Concurrent programming - singleton
- MySQL uses the method of updating linked tables with update
- MySQL union和union all区别
- Vulnhub geminiinc
猜你喜欢

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

Cadence background color setting

Excel快速跨表复制粘贴

Unity3D学习笔记5——创建子Mesh

Based on MCU, how to realize OTA differential upgrade with zero code and no development?

Redis things

PHP server interacts with redis with a large number of close_ Wait analysis

vulnhub之cereal

FL Studio 20 unlimited trial fruit arranger Download

vulnhub之raven2
随机推荐
Mmc5603nj geomagnetic sensor (Compass example)
Program process management tool -go Supervisor
DS90UB949
Vulnhub narak
Phpcms prompt message page Jump to showmessage
Ripper of vulnhub
(数据库提权——Redis)Redis未授权访问漏洞总结
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
AOSP ~ NTP ( 网络时间协议 )
解决msvcp120d.dll和msvcr120d.dll缺失
mysql使用update联表更新的方法
phpcms 提示信息頁面跳轉showmessage
CSRF
鸿蒙第三次培训(项目实训)
Web security summary
Analysis of EPS electric steering system
银泰百货点燃城市“夜经济”
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Groovy测试类 和 Junit测试
P3250 [HNOI2016] 网络 + [NECPC2022] F.Tree Path 树剖+线段树维护堆