http://oj.leetcode.com/problems/search-a-2d-matrix/
Take the matrix as a sorted list, and then use binary search directly.
class Solution {
public:
int GetValue(vector<vector<int> > &matrix, int index){
int col=matrix[0].size();
int c=index%col;
int r=index/col;
return matrix[r][c];
}
bool searchMatrix(vector<vector<int> > &matrix, int target) {
if(matrix.size()==0) return false;
int row=matrix.size(), col=matrix[0].size();
int left=0, right=row*col-1;
while(left<=right){
int mid=(left+right)/2;
int value=GetValue(matrix,mid);
if(value==target) return true;
else if(value<target) left=mid+1;
else right=mid-1;
}
return false;
}
};