class Solution {
public:
bool searchMatrix(vector<vector<int>>& mat, int target) {
int n=mat.size();
int m=mat[0].size();
int start=0;
int end=(n*m)-1;
while(start<=end){
int mid=(start+end)/2;
int row=(mid/m);
int col=mid%m;
if(mat[row][col]==target){
return true;
}
if(mat[row][col]<target){
start=mid+1;
}else end=mid-1;
}
return false;
}
};
No comments:
Post a Comment