• 剑指 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 发布!

  • 相关阅读:
    ppt技能提升
    Java中数据结构(基本数据类型+引用数据类型)介绍+整理+例子+对比
    【python】云打印实现
    【性能监控】如何有效监测网页静态资源大小?
    基于flask和fomantic-ui的简易p2p文件分享平台的手动实现
    [探究] program break (chatgpt 协助)
    从android源码分析activity的启动流程【三】
    机器学习(六)构建机器学习模型
    ARC110E Shorten ABC
    《Envoy 代理:云原生时代的流量管理》
  • 原文地址:https://blog.csdn.net/zydybaby/article/details/134388626