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

欢迎关注笔者的微信公众号


原题链接: https://leetcode.cn/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/

AC代码

class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        int i=matrix.length-1;
        int j = 0;
        while (i>=0 && j < matrix[0].length ) {
            if (matrix[i][j] == target) {
                return true;
            }
            if (matrix[i][j] > target) {
                i--;
            }else if (matrix[i][j] < target) {
                j++;
            }
        }
        return false;
    }
}

剑指 Offer 04. 二维数组中的查找_第1张图片

你可能感兴趣的:(力扣,leetcode,算法,职场和发展)