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