Accepted Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int n = matrix.size();
int m = matrix[0].size();
int l = 0, r = m * n - 1;
while (l != r){
int mid = (l + r - 1) >> 1;
if (matrix[mid / m][mid % m] < target)
l = mid + 1;
else
r = mid;
}
return matrix[r / m][r % m] == target;
}
};