Leetcode in Java && Oracle
422 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2022-03-30_15-50-07.png
42 KB
#medium

#N74. Search a 2D Matrix
problem link

#solution
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int r=matrix.length, c = matrix[0].length;
int start=0, end=r*c-1, mid=(start+end)/2;

while(start<=end){
if(matrix[mid/c][mid%c]>target)
end=mid-1;
else if(matrix[mid/c][mid%c]<target)
start=mid+1;
else
return true;

mid=(start+end)/2;
}

return false;
}
}