#include<bits/stdc++.h>
using namespace std;
typedef long long int64;


class Solution {
public:
    static bool searchMatrix(vector<vector<int>>& matrix, int target) {
        /**
         * 思路:搜索的起点很重要,如果选择从左上角搜索,这是行不通的,因为往右走会增大,往下走也会增大。
         * 可以选择以右上角作为起点,需要增大则往下走(x++),需要减小则往左走(y--)
         */
        int X = matrix.size(), Y = matrix.front().size(),  x = 0, y = Y - 1;
        while (x < X && y >= 0) {
            if (matrix[x][y] == target) return true;
            else if (matrix[x][y] < target) ++x;
            else --y;
        }
        return false;
    }
};

本站由 Rizxfrog 使用 Stellar 创建。