当前位置:网站首页>Sword finger offer04 Search in two-dimensional array [medium]
Sword finger offer04 Search in two-dimensional array [medium]
2022-07-03 12:27:00 【Wu Liuqi】
The finger of the sword Offer 04. Search in a two-dimensional array 【 secondary 】
Title Description
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 :
The existing matrix matrix as follows :
Given target = 5, return true.
Given target = 20, return false.
Limit :
0 <= n <= 1000
0 <= m <= 1000
Java Code
Violence solution
Simple and rough, two-level traversal, one by one .
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
for(int i = 0;i<matrix.length;i++){
for(int j = 0;j<matrix[i].length;j++){
if(matrix[i][j]==target){
return true;
}
}
}
return false;
}
}

Official solution
Start from the upper right corner of the two-dimensional array . If the current element is equal to the target value , Then return to true. If the current element is larger than the target value , Move to the left column . If the current element is less than the target value , Move to the next line .
Be careful : It can be proved that this method will not miss the target value . If the current element is larger than the target value , It indicates that all elements below the current element must be greater than the target value , Therefore, it is impossible to find the target value by looking down , Looking to the left may find the target value . If the current element is less than the target value , It indicates that all elements on the left of the current element must be less than the target value , Therefore, it is impossible to find the target value by looking to the left , Look down and you may find the target value .
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int row=0,column=matrix[0].length-1;
while(row<matrix.length&&column>=0){
if(matrix[row][column]==target){
return true;
}else if(matrix[row][column]<target){
row++;
}else{
column--;
}
}
return false;
}
}

Add ideas :
The key is to be able to think , Traverse from the upper right corner of the two-dimensional array .
- Because the upper left and lower right corners are the maximum and minimum values of the array respectively , All have two directions to choose at the same time , Unable to determine the unique .
- And the lower left and upper right can , Take the upper right corner for example , It is the maximum value of the line , Column minimum . If it is smaller than the target , You can exclude this line of data , Column +1( Because it is already the maximum value of this line , Smaller than the target value , It means that this line of data is too small , There can be no target value . So listed +1 Abandon .)
Another idea is :
- Look in the upper right corner . This matrix is actually like a Binary Search Tree Binary search tree .
边栏推荐
- Pragma pack syntax and usage
- (构造笔记)MIT reading部分学习心得
- error: expected reference but got (raw string)
- Qt+vtk+occt reading iges/step model
- DEJA_VU3D - Cesium功能集 之 053-地下模式效果
- Dart: About zone
- elastic_ L01_ summary
- Applet wxss introduction
- What is more elegant for flutter to log out and confirm again?
- If you can't learn, you have to learn. Jetpack compose writes an im app (II)
猜你喜欢
随机推荐
剑指Offer09. 用两个栈实现队列
laravel 时区问题timezone
The future of cloud computing cloud native
(构造笔记)ADT与OOP
实现验证码验证
网络通讯之Socket-Tcp(一)
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
Shutter widget: centerslice attribute
JVM memory model
抓包整理外篇fiddler———— 会话栏与过滤器[二]
02_ Lock the code, and don't let the "lock" become a worry
Laravel time zone timezone
Socket TCP for network communication (I)
Fluent: Engine Architecture
Dart: about grpc (I)
Implement verification code verification
Lambda expression
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
ES6新特性
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法


![[official MySQL document] deadlock](/img/2d/04e97d696f20c2524701888ea9cd10.png)






