美文网首页
74. Search a 2D Matrix

74. Search a 2D Matrix

作者: 7ccc099f4608 | 来源:发表于2020-03-10 15:17 被阅读0次

https://leetcode-cn.com/problems/search-a-2d-matrix/

image.png

(图片来源https://leetcode-cn.com/problems/search-a-2d-matrix/

日期 是否一次通过 comment
2020-03-10 1

 public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix == null || matrix.length < 1 || matrix[0].length < 1) {
            return false;
        }

        int r = matrix.length-1, c = 0;
        int re = matrix.length-1, ce = matrix[0].length-1;
        while(r >= 0 && c >= 0 && r <= re && c <= ce) {
            if(matrix[r][c] == target) {
                return true;
            }

            if(matrix[r][c] > target) {
                r --;
            } else {
                c ++;
            }
        }

        return false;
    }

相关文章

网友评论

      本文标题:74. Search a 2D Matrix

      本文链接:https://www.haomeiwen.com/subject/oswwdhtx.html