0074. Search a 2D Matrix
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
int left = 0;
int right = m*n - 1;
int mid;
while(left <= right){
mid = left + (right - left) / 2;
int row = mid / n;
int col = mid % n;
if(matrix[row][col] == target) return true;
else if(target > matrix[row][col]){
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
};