240. 搜索二维矩阵 II

240. 搜索二维矩阵 II

我是从右上角开始的。

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int i = 0;
        int j = matrix[0].length-1;

        while(i<=matrix.length-1&&j>=0){
            if(matrix[i][j]==target){
                return true;
            }else if(matrix[i][j]>target){
                j--;
            }else{
                i++;
            }
        }


        return false;        
    }
}

转载参考:
链接:https://leetcode.cn/problems/search-a-2d-matrix-ii/solutions/2361487/240-sou-suo-er-wei-ju-zhen-iitan-xin-qin-7mtf/

posted @ 2023-08-16 14:46  Chenyi_li  阅读(8)  评论(0)    收藏  举报