leetcode-----74. 搜索二维矩阵

代码

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if (matrix.empty() || matrix[0].empty()) return false;
        int n = matrix.size(), m = matrix[0].size() - 1;

        int i = 0, j = m;
        while (i < n && j >= 0) {
            if (matrix[i][j] > target) j--;
            else if (matrix[i][j] < target) i++;
            else return true;
        }
        return false;
    }
};
posted @ 2020-07-14 17:36  景云ⁿ  阅读(69)  评论(0编辑  收藏  举报