当前位置:网站首页>"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).
边栏推荐
- DS90UB949
- Arctangent entropy: the latest SCI paper in July 2022
- This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
- Uniapp implementation Click to load more
- STL tutorial 10 container commonalities and usage scenarios
- STL教程10-容器共性和使用场景
- mysql使用update联表更新的方法
- 聊聊Flink框架中的状态管理机制
- R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA
- AOSP ~ NTP ( 网络时间协议 )
猜你喜欢

Web security summary

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

AOSP ~ NTP ( 网络时间协议 )

vulnhub之Ripper

错排问题 (抽奖,发邮件)
![Capturing and sorting out external Fiddler -- Conversation bar and filter [2]](/img/04/e9cc027d753e7049f273d866eefdce.png)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]

2022年湖南工学院ACM集训第二次周测题解

聊聊Flink框架中的状态管理机制

Mmc5603nj geomagnetic sensor (Compass example)

The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
随机推荐
mysql使用update联表更新的方法
R language uses data The table package performs data aggregation statistics, calculates window statistics, calculates the median of sliding groups, and merges the generated statistical data into the o
DNS多点部署IP Anycast+BGP实战分析
Arctangent entropy: the latest SCI paper in July 2022
GCC compilation process and dynamic link library and static link library
导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
Web安全总结
Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
R language uses the aggregate function to calculate the mean value (sum) of dataframe data grouping aggregation without setting na The result of RM calculation. If the group contains the missing value
MCDF实验1
AOSP ~ NTP ( 网络时间协议 )
软考中级软件设计师该怎么备考
STL教程8-map
Unity3D学习笔记5——创建子Mesh
Go语言实现静态服务器
2022年湖南工学院ACM集训第二次周测题解
C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
银泰百货点燃城市“夜经济”