= =每行放一起,就是一个一维的数组,而且是递增的。。那用二分就好啦。。
class Solution { public: pair<int,int> toXY(int i , int row , int col){ int x = i / col; int y = i % col; return make_pair(x,y); } int getEle(const vector<vector<int> > &matrix , int i){ pair<int,int> xy = toXY(i , matrix.size() , matrix.front().size()); return matrix[xy.first][xy.second]; } bool searchMatrix(vector<vector<int> > &matrix, int target) { if(matrix.empty()) return false; int rowSize = matrix.size(); int colSize = matrix.front().size(); int l = 0; int r = rowSize * colSize - 1; while(l <= r){ int mid = (l + r) / 2; int data = getEle(matrix , mid); if(data == target) return true; if(data < target) l = mid + 1; else r = mid - 1; } return false; } };
by 1957