矩阵右上角的位置(突破口)i行j列
右上角的同行左边小于等于它,同列下面大于等于它
- 如果它大于目标值则左移动列(找小的)
- 如果小于目标值则下移行 (找大的)
class Solution {
public boolean searchArray(int[][] array, int target) {
//判断矩阵为空的情况
if(array == null || array.length == 0 || array[0].length == 0){
return false;
}
//表示矩阵右上角的位置(突破口)i行j列
//右上角的同行左边小于等于它,同列下面大于等于它
int i=0,j=array[0].length-1;
while(i <= array.length -1 && j >= 0){
int x = array[i][j];
if(x == target){
return true;
}else if(x > target){
//列左移
j--;
}else{
//行下移
i++;
}
}
return false;
}
}