思路:《剑指offer》上面有这个思路,以最右上角的元素作为参考值进行比较。
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int r = 0, c = matrix[0].size()-1; //记录行列位置
while(1){
if(matrix[r][c] == target) return true;
else if(matrix[r][c] > target)
c--;
else
r++;
if(c <0 || r >=matrix.size())return false;
}
}
};