import java.util.*;
public class Solution {
// 因为二维数组在行和列上都是递增的,所以可以考虑矩形的特殊性
// 左下角元素大于它上面的小于它右边的,右上角大于它左边的小于它下面的
// 所以可以通过类似走楼梯的方式,从左下或者右上开始查找
public boolean Find (int target, int[][] array) {
// write code here
if(array.length == 0) return false;
if(array[0].length == 0) return false;
if(target == array[array.length - 1][array[0].length - 1] || target == array[0][0]){
System.out.println(array[array.length - 1][array[0].length - 1]);
return true;
}
// 从左下开始,循环结束的判断条件是当前位置的元素索引没有越过矩阵
for(int i = array.length - 1, j = 0; i >= 0 && j < array[0].length - 1;){
// 元素较大,往上走
if(array[i][j] > target){
i--;
}else if(array[i][j] < target){
j++;
}else{
return true;
}
}
return false;
}
}