search a 2D matrix
code
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
int s = 0;
int e = m*n-1;
while(e > s+1)
{
int mid = (s+e)/2;
int x = mid/n;
int y = mid%n;
if(matrix[x][y] == target)
{
return true;
}
else if(matrix[x][y] > target)
{
e = mid;
}
else
{
s = mid;
}
}
int x = s/n;
int y = s%n;
if(matrix[x][y] == target)
{
return true;
}
x = e/n;
y = e%n;
if(matrix[x][y] == target)
{
return true;
}
return false;
}
};