搜索二维矩阵 II
参考别人的写法:原理是利用左下脚的值与target比较,如果比target大,这一行的值都比该target大;如果比target小,这一列的值都比target小
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix.length == 0)
return false;
int m = matrix.length;
int n = matrix[0].length;
int i = 0,j = n - 1;
while(i < m && j >= 0) {
if(matrix[i][j] == target)
return true;
else if(matrix[i][j] > target)
--j;
else
++i;
}
return false;
}