Binary search. Just need to convert index.
1 class Solution { 2 public: 3 bool searchMatrix(vector> &matrix, int target) { 4 if (matrix.size() == 0) return false; 5 int n = matrix.size(), m = matrix[0].size(), start = 0, end = n*m-1, mid = 0; 6 while (start <= end) { 7 mid = (start + end)/2; 8 if (matrix[mid/m][mid%m] == target) return true; 9 if (matrix[mid/m][mid%m] > target) end = mid-1;10 else start = mid+1;11 }12 return false;13 }14 };