这道题巧妙之处在于利用了右上角的数字来做对比
    如果比右上角的数字大,说明最右边的一列可以不要了,如果比右上角的数字小,说明最上面的一行可以不要了。

    1. class Solution {
    2. public:
    3. bool searchMatrix(vector<vector<int>>& matrix, int target) {
    4. if(!matrix.size() || !matrix[0].size()) return false;
    5. int r = 0, c = matrix[0].size() - 1;
    6. while(r < matrix.size() && c >= 0)
    7. {
    8. if(matrix[r][c] == target) return true;
    9. else if(matrix[r][c] < target) r++;
    10. else c--;
    11. }
    12. return false;
    13. }
    14. };