滴滴高频编程考题:搜索二维矩阵 II(中等)

题目描述

编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:

  • 每行的元素从左到右升序排列。
  • 每列的元素从上到下升序排列。

示例 1:

输入: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]], target = 5
输出:true

示例 2:

输入: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]], target = 20
输出:false

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= n, m <= 300
  • -10^{9} <= matrix[i][j] <= 10^{9}
  • 每行的所有元素从左到右升序排列
  • 每列的所有元素从上到下升序排列
  • -10^{9} <= target <= 10^{9}

解题思路

为了高效地搜索一个具有特定性质的矩阵中的目标值,我们可以利用矩阵的排序特性来设计一个时间复杂度为 O(m+n)O(m + n)O(m+n) 的算法:从矩阵的右上角或左下角开始搜索,并根据当前元素与目标值的比较结果决定搜索的方向。算法思路:

  1. 初始化:从矩阵的右上角开始。初始化 row 为 0(矩阵的行数 - 1),col 为 0(矩阵的列数 - 1)。

  2. 搜索目标值小于当前元素:由于每列的元素是升序的,目标值在当前列的上方,因此我们可以向左移动;目标值大于当前元素:由于每行的元素是升序的,目标值在当前行的下方,因此我们可以向下移动;目标值等于当前元素:找到目标值,返回 true

  3. 终止条件:当 rowcol 超出矩阵的边界时,说明目标值不在矩阵中,返回 false

复杂度分析

  • 时间复杂度: O(m+n),其中 m 是矩阵的行数,n 是矩阵的列数。最坏情况下,每个方向(向左或向下)都只遍历一次。
  • 空间复杂度: O(1),仅使用常量级别的空间来存储变量。

代码实现

package org.zyf.javabasic.letcode.hot100.matrix;

/**
 * @program: zyfboot-javabasic
 * @description: 搜索二维矩阵 II
 * @author: zhangyanfeng
 * @create: 2024-08-21 23:31
 **/
public class SearchMatrixSolution {
    public boolean searchMatrix(int[][] matrix, int target) {
        // 获取矩阵的行数和列数
        int m = matrix.length;
        int n = matrix[0].length;

        // 从矩阵的右上角开始
        int row = 0;
        int col = n - 1;

        // 搜索矩阵
        while (row < m && col >= 0) {
            if (matrix[row][col] == target) {
                return true; // 找到目标值
            } else if (matrix[row][col] > target) {
                col--; // 向左移动
            } else {
                row++; // 向下移动
            }
        }

        // 目标值不在矩阵中
        return false;
    }

    public static void main(String[] args) {
        SearchMatrixSolution solution = new SearchMatrixSolution();

        // Test Case 1
        int[][] matrix1 = {
                {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}
        };
        int target1 = 5;
        System.out.println(solution.searchMatrix(matrix1, target1)); // Expected: true

        // Test Case 2
        int[][] matrix2 = {
                {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}
        };
        int target2 = 20;
        System.out.println(solution.searchMatrix(matrix2, target2)); // Expected: false

        // Test Case 3
        int[][] matrix3 = {
                {1, 2},
                {3, 4}
        };
        int target3 = 3;
        System.out.println(solution.searchMatrix(matrix3, target3)); // Expected: true

        // Test Case 4
        int[][] matrix4 = {
                {1}
        };
        int target4 = 0;
        System.out.println(solution.searchMatrix(matrix4, target4)); // Expected: false
    }
}

具体可参考:https://zyfcodes.blog.csdn.net/article/details/141401712

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值