[LeetCode] 74. Search a 2D Matrix

OpenClaw + Qwen 模型集成飞书完整教程 1、环境要求:- **操作系统**: Windows- **Node.js**: v18+ (推荐 v20+)- **飞书账号**: 需要企业管理员权限(创建应用用)或者个人飞书账号创建个人应用- **网络**: 能访问 GitHub 和飞书开放平台2、安装Node.js下载完成后验证是否安装成功,cmd窗口输入“node --version”,显示版本号即为安装成功(如下图所示)3、获取大模型APIKEY(这里以Qwen为例) 阅读详情

74. Search a 2D Matrix (二维数组中的查找)



1. 题目翻译

给定一个m * n的二维数组和一个要查找的值,在该二维数组中查找给定的值。如果存在返回true否则返回false。

该数组有以下性质:

  1. 每行都是递增排列的
  2. 每列都是递增排列的

例子:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
target = 3, 返回 true.

2. 解题方法

二分查找的变形,将数组分成4块,如图所示

这里写图片描述

将每组的第一个值,即左上角的值与target比较

  1. 如果比target大则说明在这个分组中不可能存在target(因为每个块中左上角的值是最小的)返回false。
  2. 如果等于target就返回true。
  3. 如果小于target,需要确认这个分组中是不是只用这一个值,如果是则返回false。如果不是,使用递归继续分组。

3. 代码

class Solution {
private:
    bool search(vector<vector<int>>& matrix, int i, int j, int m, int n, int target) {
        /* i,m 是该分块中,左上角的值的坐标,j,m是该分块中,右下角值的坐标,通过这四个值可以确定分块的范围。*/
        if (i > j || m > n)               
            return false;

        if (matrix[i][m] == target)
            return true;
        else if (matrix[i][m] > target)
            return false;
        else {
            if (i >= j && m >= n)
                return false;
            int a = i+(j - i) / 2;
            int b = m+(n - m) / 2;

            //将数组分为四块,并递归求解
            return search(matrix, i, a, m, b, target) || 
                search(matrix, i, a, b + 1, n, target) ||
                search(matrix, a + 1, j, m, b, target) || 
                search(matrix, a + 1, j, b + 1, n, target);
        }
    }
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int a = matrix.size();
        if (a == 0)
            return false;
        int b = matrix[0].size();
        if (b == 0)
            return false;
        return search(matrix, 0, a-1, 0, b-1, target);

    }
};
windows10 + wsl2安装docker 一、安装wsl https://docs.microsoft.com/zh-cn/windows/wsl/install-win10 1.1以管理员身份运行powershell 1.2执行如下命令 dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart 1.3重启电脑 1.4 重启电脑后,如果需要安装wsl2, 则安装官方文档继续后续操作,因我... 阅读详情

相关推荐

香橙派5B-RK3588-NPU内核升级至0.9.8:RKLLM多模态部署实战

本文详细介绍了香橙派5B-RK3588设备NPU内核升级至0.9.8版本的完整流程,重点讲解了升级的必要性、两种升级方案(预编译包安装与自行编译内核)以及RKLLM SDK的安装配置。通过实战演示多模态模型转换与推理,显著提升AI计算性能,为嵌入式AI部署提供实用指南。

salt9的博客 1025

74. Search a 2D Matrix

74.Search a 2D Matrix Medium 3135195Add to ListShare Write an efficient algorithm that searches for a value in anm x nmatrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each ro...

Sterben_Da的博客 170

Ghost备份还原Win10全攻略:从老电脑迁移到新硬盘,ESP分区处理是关键

本文详细介绍了使用Ghost工具将Win10系统从旧电脑迁移到新硬盘的全过程,特别强调了ESP分区的关键处理方法。通过分步指导备份、分区准备、系统还原和引导修复,帮助用户实现无缝系统迁移,解决常见问题如启动失败和驱动兼容性。

weixin_30823001的博客 672

74. 搜索二维矩阵(Search a 2D Matrix)

74. 搜索二维矩阵(Search a 2D Matrix)题解两次二分查找复杂度分析PythonJava(待完成)一次二分查找算法流程复杂度分析PythonJava(待完成) 题解 看到本题矩阵的性质,第一反应是使用两次二分查找。第一次查询,targettargettarget的所在行。第二次查询,targettargettarget的所在列。 两次二分查找 特判,若matrixmatrixm...

朱师傅的博客 353

ET篇:Google.Protobuf的学习(理解基本的使用流程)

据我目前所知,ET使用的数据交换协议是Google.Protobuf,所以今天就来学习一下,但是网上许多人都是以高下立判的方式讲解的,对新手小白很不友好,所以我今天就以纯小白的视角和大家一起学习 学习环境: .NET Core2.2 .NET Framework 4.7.2 Rider protobuf-csharp-3.7.0-rc-2 Google.Protobuf简介 prot...

设计师养成记 3563

Ubuntu 22.04蓝牙开关秒关?别慌,可能是这个Intel固件文件在搞鬼

本文深入解析Ubuntu 22.04蓝牙开关秒关问题,指出Intel固件文件缺失是常见原因,并提供详细修复方案。通过确认硬件型号、检查固件文件及智能替代方案,帮助用户快速解决蓝牙故障,提升系统稳定性。

weixin_30247307的博客 755

[leetcode] 74. Search a 2D Matrix 解题报告

题目链接:https://leetcode.com/problems/search-a-2d-matrix/ Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each

小榕流光的专栏 1022

基于OpenCV的驾驶疲劳检测与预警系统设计

PERCLOS(Percentage of Eye Closure)是一种基于眼睑闭合程度的疲劳度量指标。在基于OpenCV的驾驶疲劳检测与预警系统设计中,PERCLOS可以用于检测驾驶员的眼睑闭合程度,以判断其是否疲劳。该函数用于检测视频流中人脸的疲劳程度。具体地,该函数通过检测人脸的眼睛和嘴巴的状态来评估人脸的疲劳程度。眼睛状态是通过计算眼睛的长宽比(eyear)来判断,如果长宽比小于某个阈值,则认为眼睛闭合,表示人处于疲劳状态。

2301_79810943的博客 2327

LeetCode 74. Search a 2D Matrix(二分)

题目来源:https://leetcode.com/problems/search-a-2d-matrix/ 问题描述 74.Search a 2D Matrix Medium Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following p...

da_kao_la的博客 168

手把手教你用GEM-2多频电磁仪找地下管道(附真实数据图)

本文详细解析了使用GEM-2多频电磁仪精准定位地下管线的全流程,包括设备调试、频率选择、现场扫描技巧及数据解读。通过真实案例展示了如何应对复杂干扰环境,提升管线检测效率与准确性,特别适合市政工程和地下管线普查。

weixin_42565652的博客 396

【超详细】安装了anaconda后,Ubuntu18+COLMAP配置疯狂踩坑踩至魔怔的记录

本博客收集了博主在Ubuntu18系统中编译COLMAP所遇到的困难,提供了解决方法,也收集了目前网上大多数的坑,除此之外,也为Ubuntu18系统下COLMAP的安装提供了一种新的路径。

Stay_Foo_lish的博客 3509

LeetCode: 74. Search a 2D Matrix

题目:74. Search a 2D Matrix(https://leetcode.com/problems/search-a-2d-matrix/description/) 解法: 将二维数组看成一维数组,元素的坐标可以计算出来。此解法使用二分查找。 public class Solution { public boolean searchMatrix(int[][] matrix...

majinliang1234的博客 171

【软件系统架构】系列四:嵌入式软件-NPU(神经网络处理器)系统及模板

NPU(Neural Processing Unit) 是专门用于人工神经网络推理与训练的处理器,具备高并行、低功耗、低延迟等特点。它是边缘 AI、自动驾驶、智能摄像头、语音识别等场景中的关键硬件。

moton2017的博客 2248

LeetCode74——Search a 2D Matrix

LeetCode74——Search a 2D Matrix 二维数组查找问题,二重循环暴力搜索肯定是超时的,好在二维数组有一定的规律: 1.每一行从左到右是非递减的 2.每一列从上到下是非递减的 那么根据这个条件我们很容易得出: 1.给定一个数 number 如果他比矩阵中的某个数nums[i][j]大,则存在两种情况: 这个数要么在nums[i][j]的右侧,要么在nums

上善若水 1432

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

leetcode.array--74. Search a 2D Matrix

题目:74. Search a 2D Matrix 题目链接:https://leetcode.com/problems/search-a-2d-matrix/description/ 给定二维数组,数组每一行均为升序,且每一行的第一个元素均小于上一行最后一个元素。在数组中查找target,返回True或者False。其实两次二分查找就可以找到了,第一次二分查找去定位target所在的行,第二

Nero Zhang的博客 318

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LeetCode 74. Search a 2D Matrix

整体来说,你一开始提出的"先锁定行,再二分列"的思路在大方向上是完全正确的,只需要在边界和下标上更严谨,同时理解官方常用的一维化写法,可以在面试中灵活切换表达。

六六哥的博客 777

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

[LeetCode 74] Search a 2D Matrix

题目链接:search-a-2d-matrix /** * Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from le

瓦尔登湖 868

【LEETCODE】74-Search a 2D Matrix [Python]

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right.The first integer of each ro

aliceyangxi1987的博客 1396

【LeetCode】解题74:Search a 2D Matrix

LeetCode解题 74:Search a 2D Matrix Problem 74: Search a 2D Matrix [Medium]解题思路Solution (Java) Problem 74: Search a 2D Matrix [Medium] Write an efficient algorithm that searches for a value in an m x n m...

Fayedily的博客 243

LeetCode74 Search a 2D Matrix

LeetCode74 Search a 2D Matrix问题描述 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left t

yanqinghe123的专栏 229

LeetCode 74. Search a 2D Matrix 题解

本题是二分查找的经典变体问题,主要考察对二分查找算法的理解和应用。通过将二维矩阵看作一维有序数组,我们可以使用二分查找来高效地定位目标值。二分查找的核心思想是:使用两个指针分别指向查找区间的两端,计算中间位置,比较中间元素与目标值的大小,根据比较结果缩小查找区间,直到找到目标值或确定目标值不存在。这种方法的时间复杂度为 O(log(m * n)),适用于大规模二维矩阵的查找。掌握二分查找的原理,对于理解搜索算法和算法设计思想非常重要。

cannonjinx的博客 4280

Leetcode 74 Search a 2D Matrix

Leetcode 74 Search a 2D Matrix题目思路代码 题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than t

weixin_43796689的博客 145

LeetCode 74(Search a 2D Matrix)Java

原题:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties 1. Integers in each row are sorted from left to right. 2. The first integer o

Jassy1994的博客 528

Leetcode 74. Search a 2D Matrix

题目描述:给定一个矩阵,找出矩阵中是否有某个target,有的话就True没有的话就是False。矩阵每一行是单调递增的,下一行的第一个数比上一行的最后一个数要大。 题目链接:Leetcode 74. Search a 2D Matrix 我的思路就是首先用O(n)时间访问记录 每一行的开始和结尾,然后传入二分查找,只不过搜索的维度有多了一个,就像那个二叉搜索树查找公共父节点的题目一样。 ...

Za七杂⑧ 232

leetcode 74. Search a 2D Matrix【Java】

题目描述 搜索二维矩阵 AC代码 class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix.length==0||matrix[0].length==0) return false; int rows=matrix.leng...

NayelyA的博客 244
上一篇: [操作系统] 文件系统基本概念
下一篇: [LeetCode] 206. Reverse Linked List
LLLZHLIU
博客等级 码龄10年 19粉丝 43原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值