当前位置:网站首页>"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).
边栏推荐
- AOSP ~ NTP ( 网络时间协议 )
- Based on MCU, how to realize OTA differential upgrade with zero code and no development?
- vulnhub之GeminiInc v2
- 简单工厂和工厂方法模式
- R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA
- Kubernetes 三打探针及探针方式
- vulnhub之cereal
- PHP server interacts with redis with a large number of close_ Wait analysis
- After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
- Numpy np. Max and np Maximum implements the relu function
猜你喜欢

《剑指offer 04》二维数组查找

DS90UB949

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

The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation

PHP Basics

STL教程9-容器元素深拷贝和浅拷贝问题

Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power

The excel table is transferred to word, and the table does not exceed the edge paper range

(数据库提权——Redis)Redis未授权访问漏洞总结

vulnhub之presidential
随机推荐
导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
.\vmware-vdiskmanager.exe -k “c:\\xxxxx.vmdk”
Excel表格转到Word中,表格不超边缘纸张范围
Web security summary
How to make others fear you
鸿蒙第四次培训
Phpcms prompt message page Jump showmessage
Excel快速跨表复制粘贴
libvirt 中体验容器
STL教程10-容器共性和使用场景
量化计算调研
vulnhub之Ripper
在CoreOS下部署WordPress实例教程
Numpy np.max和np.maximum实现relu函数
Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
动态规划(区间dp)
GCC compilation process and dynamic link library and static link library
ASP.NET-酒店管理系統
小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation