240. Search a 2D Matrix II
刷
June-21-2019
这个题居然也没记录过?
没法直接二分,因为不是完全排列的。
左下开始
O(row + col)
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
int row = matrix.length - 1;
int col = 0;
while (row >= 0 && col < matrix[0].length) {
int tempVal = matrix[row][col];
if (tempVal == target) {
return true;
} else if (tempVal > target) {
row --;
} else {
col ++;
}
}
return false;
}

浙公网安备 33010602011771号