LeetCode 74. 搜索二维矩阵

题目链接

74. 搜索二维矩阵

剑指 Offer 04. 二维数组中的查找一样

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        std::ios::sync_with_stdio(false);
        if(matrix.empty()) return false;
        int i = 0, j = matrix[0].size() - 1, n = matrix.size();
        while(i < n && j >= 0){
            if(matrix[i][j] == target) return true;
            if(matrix[i][j] > target) j--;
            else i++;
        }
        return false;
    }
};
posted @ 2021-03-30 09:20  蒟蒻颖  阅读(20)  评论(0编辑  收藏  举报