从右上角或左下角开始,类似二叉搜索树BST
/**
* 从右上角或者左上角开始运算,这样才能保证两个方向一个是变大一个是变小
* Runtime: 4 ms, faster than 100.00%
* Memory Usage: 44.8 MB, less than 31.44%
*/
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int i = 0, j = matrix[0].length - 1;
while (i < matrix.length && j >= 0) {
if (matrix[i][j] == target) {
return true;
}
if (target > matrix[i][j]) {
i++;
} else {
j--;
}
}
return false;
}
}