240. 搜索二维矩阵 II 力扣(中等) Z字型查找

240. 搜索二维矩阵 II

编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:

每行的元素从左到右升序排列。
每列的元素从上到下升序排列。
 实例1:

题解:https://leetcode-cn.com/problems/search-a-2d-matrix-ii/solution/sou-suo-er-wei-ju-zhen-ii-by-leetcode-so-9hcx/

代码:

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int x=0;
        int y=matrix[0].size()-1;
        bool flag=0;
        while(x<matrix.size() && y>=0)
        {
            if(matrix[x][y]==target) {flag=1; break;}
            if(matrix[x][y]<target) x++;
              else y--;
        }
        return flag;
    }
};
原文地址:https://www.cnblogs.com/stepping/p/15463397.html