算法-有序矩阵中找目标值

这篇博客讨论了如何在给定的有序二维矩阵中高效地寻找目标值。通过利用矩阵的有序特性,可以将时间复杂度降低到O(N+M),其中N是行数,M是列数。从右上角开始,根据比较结果确定搜索方向,最终找到目标值或返回False。此外,还提到了LeetCode的相关题目链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:

 You are given a two-dimensional array (matrix) of distinct integers where each row is sorted and each column is also sorted. The matrix does not necessarily have the same height and width. You are also given a target number, and you must write a function that returns an array of the row and column indices of the target number if it is contained in the matrix and [-1, -1] if it is not contained in the matrix.

题目大致意识是,给定一个有序的二维矩阵,然后给定一个目标值,想从这个有序矩阵中,找到这个目标值,如果没有找到的话,返回False, 找到返回True。
该有序的二维矩阵,从左到右行有序,从上到下列有序:
在这里插入图片描述

以上图为例,假设taget num是44,矩阵中的位置是第3行第3列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uncle_ll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值