classSolution { public: boolsearchMatrix(vector<vector<int>>& matrix, int target){ if(matrix.empty() || matrix[0].empty()) returnfalse; int n = matrix.size(), m = matrix[0].size(); vector<int> v(n * m); int k = 0; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) v[k++] = matrix[i][j]; int l = 0, r = k - 1; while(l < r){ int mid = l + r >> 1; if(v[mid] >= target) r = mid; else l = mid + 1; } return v[r] == target; } };
classSolution { public: boolsearchMatrix(vector<vector<int>>& matrix, int target){ if(matrix.empty() || matrix[0].empty()) returnfalse; int n = matrix.size(), m = matrix[0].size(); int l = 0, r = n * m - 1; while(l < r){ int mid = l + r >> 1; if(matrix[mid / m][mid % m] >= target) r = mid; else l = mid + 1; } return matrix[r / m][r % m] == target; } };