当前位置:网站首页>"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
vulnhub之GeminiInc
Modular programming of single chip microcomputer
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
Slam mapping and autonomous navigation simulation based on turnlebot3
Web安全总结
Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
STL tutorial 10 container commonalities and usage scenarios
After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
随机推荐
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
R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)
vulnhub之Ripper
Notes on 32-96 questions of sword finger offer
鸿蒙第三次培训(项目实训)
Qt+VTK+OCCT读取IGES/STEP模型
外插散点数据
MySQL uses the method of updating linked tables with update
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 world's most popular font editor FontCreator tool
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
Ripper of vulnhub
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
Web安全总结
vulnhub之cereal
软考中级软件设计师该怎么备考
836. Merge sets (day 63) and search sets
基于turtlebot3实现SLAM建图及自主导航仿真