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


    tags:

    • 数组
    • 线性查找 categories:
    • 算法
    • 剑指 Offer

    题目描述

    在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    示例:

    现有矩阵 matrix 如下:

    [
      [1,   4,  7, 11, 15],
      [2,   5,  8, 12, 19],
      [3,   6,  9, 16, 22],
      [10, 13, 14, 17, 24],
      [18, 21, 23, 26, 30]
    ]
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    给定 target = $5$,返回 $true$。

    给定 target = $20$,返回 $false$。

    限制:

    $0 <= n <= 1000$

    $0 <= m <= 1000$

    注意: 本题与主站 240 题相同:https://leetcode-cn.com/problems/search-a-2d-matrix-ii/


    算法

    (线性查找) $O(n)$

    根据题目描述,每一行的数是单调递增的,每一列的数也是单调递增的,由这个性质我们以矩阵的右上角作为边界开始查找,分三种情况:

    1. 当前数大于 $target$,那么 $j --$ 从前一列继续寻找
    2. 当前数小于 $target$,那么 $i ++$ 从下一行继续寻找
    3. 如果相等,则返回 $true$

    最后如果没找到则返回 $false$。

    时间复杂度

    $O(n)$

    空间复杂度

    $O(1)$

    C++ 代码
    class Solution {
    public:
        bool findNumberIn2DArray(vector>& matrix, int target) {
            if (matrix.empty() || matrix[0].empty()) return false;
    
            int n = matrix.size(), m = matrix[0].size();
            int i = 0, j = m - 1;
            while (i < n && j >= 0) {
                if (matrix[i][j] < target) i ++ ;
                else if (matrix[i][j] > target) j -- ;
                else return true;
            }
    
            return false;
        }
    };
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    Java 代码
    class Solution {
        public boolean findNumberIn2DArray(int[][] matrix, int target) {
            if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
                return false;
            }
    
            int n = matrix.length, m = matrix[0].length;
            int i = 0, j = m - 1;
            while (i < n && j >= 0) {
                if (matrix[i][j] < target) {
                    i ++ ;
                } else if (matrix[i][j] > target) {
                    j -- ;
                } else {
                    return true;
                }
            }
    
            return false;
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    Python 代码
    class Solution:
        def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
            if not matrix or not matrix[0]:
                return False
    
            n, m = len(matrix), len(matrix[0])
            i, j = 0, m - 1
            while i < n and j >= 0:
                if matrix[i][j] < target:
                    i += 1
                elif matrix[i][j] > target:
                    j -= 1
                else:
                    return True
    
            return False
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    推荐阅读

    本文由博客一文多发平台 OpenWrite 发布!

  • 相关阅读:
    ArcGIS_将多个点数据整合成一个点数据
    下沉一线农技志愿服务 国稻种芯-芜湖:湾沚红杨护秋粮生产
    SVN版本控制软件
    【学习笔记】拟阵
    大家都能看得懂的源码之ahooks useInfiniteScroll
    MySQL进阶学习
    【web前端期末大作业】基于html+css+javascript+jquery技术设计的音乐网站(44页)
    在IDEA里面操作Git
    计算机算法分析与设计(9)---0-1背包问题(含C++代码)
    数据库之-元数据 DatabaseMetaData 初学
  • 原文地址:https://blog.csdn.net/zydybaby/article/details/134388626