LeetCode系列240—搜索二维矩阵(2)

题意

240. 搜索二维矩阵 II

题解

Z字形查找

class Solution {
    
    
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
    
    
        int m = matrix.size(), n = matrix[0].size();
        int i = 0, j = n - 1;
        while (i < m && j > -1) {
    
    
            if(matrix[i][j] == target) return true;
            else if (matrix[i][j] > target) j--;
            else i++;
        }
        return false;
    }
};

猜你喜欢

转载自blog.csdn.net/younothings/article/details/121452010