74. Search a 2D Matrix
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int start = 0;
int columnN= matrix[0].length;
int end = matrix.length*columnN;
while(start<end){
int mid = (start+end)/2;
// important! use number of column or row!
int i=mid/columnN;
int j= mid%columnN;
if(matrix[i][j] == target){
return true;
}else if(matrix[i][j]<target){
start = mid+1;
}else{
end =mid;
}
}
return false;
}
}
Last updated
Was this helpful?