来源:牛客
讲解:剑指offer P44
public class Solution {
public boolean Find(int target, int [][] array) {
boolean found = false;
int col = array[0].length - 1;
int row = 0;
while(row < array.length && col >= 0) {
//最右上角的大于给定值,排除最右列,col--
if(array[row][col] > target){
col--;
}else if(array[row][col] < target){
//最右上角的小于给定值,排除当前行,row++
row++;
}else if(array[row][col] == target){
found = true;
break;
}
}
return found;
}
}
或者
public class Solution {
public boolean Find(int target, int [][] array) {
boolean found = false;
int colum = array[0].length - 1;
int row = 0;
while(row < array.length && colum >= 0){
if(array[row][colum] == target){
found = true;
break;
}else if(array[row][colum] > target){
colum--;
}else{
row++;
}
}
return found;
}
}