当前位置:网站首页>LeetCode 74. Search 2D matrix
LeetCode 74. Search 2D matrix
2022-07-04 08:56:00 【Sean_ Asu】
Today I saw the dichotomy summary formula of snow vegetables , I'm confused 3 I understand it a little , Simple dichotomy can be slightly understood , Search the binary matrix , I still have a little trouble , Take advantage of some ideas , Make notes for your own review .
The original title is :
Power button -74. Search the binary matrix
First of all, let's mention the dichotomy summary template of the snow cabbage boss :
Snow vegetable - Two sub topics You can take a step-by-step look at the two-part explanation here
Binary search algorithm template - Snow vegetable You can move here to see the binary template

This topic , Because all numbers are increasing in order , So you can use dichotomy
Because it's a two-dimensional array , It needs to be found in the middle middle, So first write the number of rows and columns
The two-dimensional array is shown in the figure :

Then its column number is a two-dimensional array matrix The length of : matrix.length, The number of rows is the length of the first column :matrix[0].length
Then set the leftmost left from 0 Start , Far right right It is That's ok × Column -1, Median middle It is left + (right - left) /2
then Set a check(t) function , there t Set the conditions yourself , Because all the numbers in this question are Orderly increase , I set For this condition A random number >=target, Because it's a binary array , So this number is :matrix[mid /m][mid % m] , So I if The judgment condition is matrix[mid /m][mid % m] >= target
Draw the picture according to this

here , According to your own conditions , take [L , R] It is divided into two paragraphs , On the far right is >=target Of , Then assume the midpoint middle On the right side , that It can be seen that , The left boundary of the right range is at the midpoint middle, Ask for this boundary , Its next screening range is [L , R] Turned into [ L , middle ] , If middle Fall on On the left side That is, as shown in the figure

Because the boundary you are looking for is middle The right side of the , And it doesn't belong to >target The scope of the , Then the screening range is determined by [L , R] Turned into [ middle +1 , R ] , This will confirm if-else Of The executive part of
Again , If you choose the condition opposite to the above <=target , Then the scope at this time is the part on the left , You need to find the right boundary of the left part , If at this time middle Fell on the right side , Then the screening range is determined by [L , R] Turned into [ L , middle -1 ], If it falls on the left , Then the screening range is determined by [L , R] Turned into [ middle , R ]
Finally, analyze and judge , When the array is empty , I can't find it at this time target, Then you have to return false:if(matrix.length==0 || matrix[0].length==0) return false;
If the array is not empty , But I can't find target, Return, too false:
if(matrix[r /m][r % m] != target) return false;
A certain number of two-dimensional arrays Abscissa 、 Vertical coordinate solution :
cross : Ordinal number / Row number , In this way, we can get the number of lines
longitudinal : Ordinal number % Row number , In this way, we can get the number of columns
for instance :

In the picture 16 Of coordinate , Then press from 0 Start in order ,16 The order number of should be 6, Then use 6 Go to the above to get :6 / 4 Rounding down =1,6 % 4=2
So at this time 16 The coordinate position of is :[ 1 , 2 ] first line , Second column ( That's ok 、 Columns are from 0 Start )
In this way, you can write code
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix.length==0 || matrix[0].length==0) return false;
int n =matrix.length,m=matrix[0].length; //n Is the number of columns ,m Is the number of rows
int l =0, r=m *n -1;
while(l <r){
int mid = l + r >>1;
if(matrix[mid /m][mid % m] >= target) r = mid;
else l = mid+1;
}
if(matrix[r /m][r % m] != target) return false;
else return true;
}
}边栏推荐
- UML 时序图[通俗易懂]
- How to send pictures to the server in the form of file stream through the upload control of antd
- Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
- Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
- Educational Codeforces Round 119 (Rated for Div. 2)
- Awk from entry to soil (5) simple condition matching
- China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
- Getting started with microservices: gateway gateway
- manjaro安装微信
- 如何编写单元测试用例
猜你喜欢

4 small ways to make your Tiktok video clearer

What if I forget the router password

Explain TCP protocol in detail three handshakes and four waves

C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code

Codeforces Global Round 21(A-E)

User login function: simple but difficult

Developers really review CSDN question and answer function, and there are many improvements~
![C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)

Codeforces Global Round 21(A-E)
![Leetcode topic [array] -136- numbers that appear only once](/img/6d/f2e4b812e5dd872fbeb43732d6f27f.jpg)
Leetcode topic [array] -136- numbers that appear only once
随机推荐
Awk from digging into the ground to getting started (10) awk built-in functions
Awk from getting started to digging in (9) circular statement
Newh3c - routing protocol (RIP, OSPF)
The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
LinkedList in the list set is stored in order
Educational Codeforces Round 119 (Rated for Div. 2)
CLion-控制台输出中文乱码
Awk from entry to earth (12) awk can also write scripts to replace the shell
How to solve the problem of computer jam and slow down
awk从入土到入门(10)awk内置函数
awk从入门到入土(6)正则匹配
【无标题】转发最小二乘法
Research Report on the current market situation and development prospects of calcium sulfate whiskers in China (2022 Edition)
Codeforces Round #793 (Div. 2)(A-D)
如何通过antd的upload控件,将图片以文件流的形式发送给服务器
In depth research and investment strategy report on China's hydraulic parts industry (2022 Edition)
awk从入门到入土(14)awk输出重定向
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
High order phase difference such as smear caused by myopic surgery
std::is_ union,std::is_ class,std::integral_ constant