LeetCode | 74. Search a 2D Matrix

RandLA-Net Tensorrt 推理 c++ RandLA-Net 实现 Tensorrt 部署. 阅读详情

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 the last integer of the previous row.

For example,

Consider the following matrix:

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

思路:二分,复杂度O(log(m+n))

Solution

代码一:先对行二分,再对列二分

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int row = -1, col = -1;
        int m = matrix.size();

        if(m == 0)
            return false;

        int n = matrix[0].size();

        if(n == 0)
            return false;

        //先找在哪一行
        int left = 0, right = m-1;
        while(left <= right)
        {
            int mid = (left+right)/2;
            if(matrix[mid][0] > target)
                right = mid-1;
            else
            {
                if(mid == m-1)  //已经是最后一行
                {
                    row = mid; break;
                }
                else if(matrix[mid+1][0] > target)
                {
                    row = mid; break;
                }
                else
                    left = mid+1;
            }
        }
        if(row == -1)
            return false;
        left = 0; right = n-1;
        while(left <= right)
        {
            int mid = (left+right)/2;
            if(matrix[row][mid] == target)
            {
                col = mid; break;
            }
            else if(matrix[row][mid] > target)
                right = mid-1;
            else
                left = mid+1;
        }

        if(col == -1)
            return false;
        return true;
    }
};

代码二:将矩阵展开,当做一个有序列表,然后直接查找

class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        int n = matrix.size();
        if(n == 0)
            return false;
        int m = matrix[0].size();\
        if(m == 0)
            return false;

        int l = 0, r = m * n - 1;
        while (l != r){
            int mid = (l + r - 1) >> 1;
            if (matrix[mid / m][mid % m] < target)
                l = mid + 1;
            else 
                r = mid;
        }
        return matrix[r / m][r % m] == target;
    }
};
LPDDR6 Efficiency Mode 技术探讨 阅读详情

相关推荐

黑群晖二合一已损毁_手动修复黑群晖已损毁磁盘空间

lz的用法是这样的,一个中古级mac mini 2011上装有vmware esxi 7.6, 然后上面挂载了lz的华芸Asustor Nas的iscsi空间,lz用的是basic volume,然后esxi上面有虚拟的黑群晖6.2.2-24922,黑群晖的docker里跑着不可描述软件,但是发现下载速度过快时,比如有好几个文件同时下载,总速度超过60MB/s以后storage manager里很...

weixin_39669629的博客 1万+

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的博客 169

302.QoS实现工具之MQC

QoS 技术可以对网络中报文进行分类处理,根据优先级提供不同的差分服务,如何实现这种差分服务呢?我们有一种强大的配置方法 - 模块化 QoS 命令行 MQC(Modular QoS Command-Line)。下面就来介绍一下 MQC。

迷逝 1397

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

微信群“智”变:扣子机器人无缝接入实战

本文介绍了如何将扣子(Coze)机器人接入微信群实现智能化管理。文章首先阐述了扣子机器人的自然语言处理、自动分类和多模态交互等功能特点,以及其深度学习模型和持续学习能力的技术优势。随后详细讲解了接入步骤:从微信公众号配置、扣子平台机器人创建,到使用Flask框架实现消息转发功能的关键代码。通过电商客服和技术社群两个实际案例,展示了扣子机器人在提升响应效率、优化社群运营方面的显著效果。最后展望了未来在智能推荐、多模态交互等领域的发展潜力,为微信群智能化管理提供了实用解决方案。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 833

NXP S32k3 MCAL EMIOS PWM配置

NXP s32k 配置 EMIOS PWM

Leon_0125的博客 2704

LeetCode74——Search a 2D Matrix

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

上善若水 1432

FPGA上的时间数字转换器(TDC):详细概述及FPGA开发

本文对基于FPGA的时间数字转换器(TDC)进行了详细的概述,并提供了一个简单的示例代码,展示了如何在FPGA上实现TDC功能。在本文中,我们将详细概述基于FPGA的TDC,并提供相应的源代码示例,以帮助读者理解和实现这一功能。需要注意的是,这只是一个简单的示例,实际的TDC设计可能需要更多的功能和优化,以满足特定的需求。读取和重置:最后,测量结果可以通过读取计数器的值来获取,并且在每次读取后需要将计数器重置为零,以便进行下一次测量。TDC的基本原理是测量信号的到达时间与参考时钟之间的时间差。

EsqpWeller的博客 6872

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

Android 15.0 系统开机屏幕设置默认横屏显示

在15.0的系统ROM产品定制化开发中,对于产品需求来说,由于是宽屏设备所以产品需要开机默认横屏显示,开机横屏显示这就需要从 两部分来实现,一部分是系统开机动画横屏显示,另一部分是系统屏幕显示横屏显示,从这两方面就可以做到开机默认横屏显示了

安卓兼职framework和app工程师的博客 722

Spring AI 实战:第二章、Spring AI提示词之玩转AI占卜的艺术

引言:程序员的神秘学探索引言:程序员的神秘学探索“下个迭代能按时发布吗?“这次晋升机会有多大?“我写的代码为什么总在半夜出bug?作为一名开发者是否也曾被这些略带玄学问题困扰?那让我们用Spring AI和提示词工程打造一个专属于程序员的"赛博占卜师",让大师来为你占卜一卦吧~一、提示词工程 - 与AI对话的艺术1.1 提示词工程是什么类比一下,AI就像一位刚从名校毕业的高材生它知识渊博但缺乏经验能解决复杂问题但需要明确指引有时会"过度脑补"(专业术语叫"幻觉")

liaokailin的专栏 1855

LeetCode 74. Search a 2D Matrix

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

六六哥的博客 777

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

[LeetCode] 74. Search a 2D Matrix

Problems: https://leetcode.com/problems/search-a-2d-matrix/ Solution1: class Solution { public boolean searchMatrix(int[][] matrix, int target) { // 矩阵为空,需要注意的是depth和width都需要判断 if(mat...

Renoler的博客 199

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

[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

瓦尔登湖 869

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

【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 题解

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

cannonjinx的博客 4280

【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 解题报告

题目链接: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

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

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

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

Za七杂⑧ 233

Leetcode 74搜索二维矩阵 search a 2D matrix

给你一个整数 target ,如果 target 在矩阵中,返回 true;否则,返回 false。

imlxinyu的博客 773

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解题报告】74. Search a 2D Matrix

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

Angry_Bird16的博客 322

【Leetcode】74 Search a 2D Matrix

74. Search a 2D Matrix Total Accepted: 84883Total Submissions: 246852Difficulty: Medium Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has

renxingzhadan的专栏 321
上一篇: LeetCode | 73. Set Matrix Zeroes
下一篇: LeetCode | 75. Sort Colors
PKU_CXK
博客等级 码龄10年 87粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值