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 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.

代码:

方法一

利用有序矩阵的性质(行递增,列递增),从左下角往右上角搜索;比当前值小,往上搜索;比当前值大,往右搜索。从右上角往左下角搜索类似。

class Solution {
public:
    //由于行增和列增的性质,可从左下角开始,向上递减,向右递增
    //以左下为起点,往右上搜索,如果target比当前m[row][column]小,则row--,否则column++;相等返回true
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        ////提前得到size(),127个测试集,24ms->12ms
        int m=matrix.size(),n=matrix[0].size();
        for(int row=m-1,column=0;row>=0 && column<n;){
            if(target<matrix[row][column]){
                row--;
            }else if(target>matrix[row][column]){
                column++;;
            }else{
                return true;
            }
        }
        return false;
    }
};

上面代码的时间复杂度:O(m+n)

方法二

把二维矩阵看成一维数组,用二分查找

class Solution {
public:
    //把整个矩阵看成一行,二分查找
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m=matrix.size(),n=matrix[0].size();
        int low=0,high=m*n-1,middle,i,j;
        while(low<=high){
            middle=(low+high)/2;
            i=middle/n;
            j=middle%n;
            if(target<matrix[i][j]){
                high=middle-1;
            }else if(target>matrix[i][j]){
                low=middle+1;
            }else{
                return true;
            }
        }
        return false;
    }

};

时间复杂度:O(log(m*n)),m为行数,n为列数。

关于solidworks无法获得下列许可SOLIDWORKS Standard的解决方法 在使用solid works的时候,出现无法获得下列许可SOLIDWORKS Standard,刚开始还以为软件出了问题,经过一方折腾找到了解决方法。希望这个方法可以帮助到您 找到这个服务 问题到这里就已经得到了解决,但更大的问题是,要是每次遇见这种都要这么麻烦,岂不是很累?这里也提供一种方法。考虑到每个人的情况,下面说的可能有一点啰嗦。 !!!!!!注意那个在此期间之后重新启动服... 阅读详情

相关推荐

GSM的“时间管理大师”:TDMA时隙、突发脉冲与逻辑信道映射实战图解

本文深入解析GSM网络中的TDMA时隙调度与逻辑信道映射技术,揭示2G网络如何通过精密的时间管理和突发脉冲设计实现高效通信。从TDMA帧结构到GMSK调制优化,详细介绍了GSM的工程艺术及其在现代通信中的延续价值。

weixin_30631587的博客 514

leetcode, 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。给定一个目标数代码 行从左到右递增,列从上到下递增,找目标值

字节 讲思路在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序,给定一个目标数,判断是否在矩阵中。 先看target是不是大于第一行最右边的(最大值), 大于然后对比往下一行的最大值, 小于就跟当前一列的值往左对比。。 二维数组是从左到右递增,从上到下递增,row为行数,col为列数,a[row-1][col-1]为最大数,如果剔除这一行,这一列,则最大数为a[row-2][col-2],如果要查找的num大于a[row-2][col-2]并且

weixin_45503449的博客 2579

S-function入门及案例详解(1)——S-function基础介绍及基本案例

本专题将用三篇文章对S-function进行介绍及其案例详解。欢迎关注~ 目录一、S-Function简介二、详解sfuntmpl函数2.1 sfuntmpl函数介绍2.2 mdlInitializeSizes函数2.3 mdlDerivatives函数2.4 mdlUpdate函数2.5 mdlOutputs函数2.6 mdlGetTimeOfNextVarHit函数2.7 mdlTerminate函数三、案例1——对波形的幅值进行放大3.1 案例分析3.2 案例详解3.3 输出波形四、案例2——对波形的

Wendy的博客 7万+

力扣(LeetCode) 剑指Offer 04

题目描述 在一个 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] ] 给定 target = 5,返回 true

weixin_43754153的博客 198

项目管理中 PMO 的关键职能解析

在企业数字化转型与战略落地的浪潮中,PMO(Project Management Office,项目管理办公室)逐渐从“项目辅助角色”升级为“组织级战略执行中枢”。然而,多数人对PMO的认知仍停留在“管项目的部门”或“文档审核机器”的层面。本文将打破误解,通过生活化类比(如“中枢神经”“导航系统”)、流程图解(战略对齐循环)、实际案例(制造企业PMO升级),深入解析PMO的五大核心职能:战略对齐、流程标准化、资源管理、绩效监控、知识沉淀。最终揭示PMO的本质——

项目管理的博客 1252

基于深度学习的电力系统暂态稳定评估与预防控制复现

本文复现了基于深度学习的电力系统暂态稳定评估与预防控制方法。主要内容包括:(1)数据预处理环节,对IEEE 39节点系统的电压幅值、相角等特征数据进行归一化处理;(2)ACNGAT模型实现,该模型结合时间卷积网络(TCN)和图注意力网络(GAT)提取时空特征;(3)采用改进的帝企鹅优化算法进行预防控制。关键技术点包括:融合时空信息的特征提取、改进的邻接矩阵构建方法(λ_ij = E_i * E_j * B_ij)以及时空注意力机制。代码实现采用Python和PyTorch框架。

huanghm88的专栏 336

【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

【即插即用模块】Transformer篇 | TCSVT 2025 | DAFM:双注意力聚合+反向跨模态筛选,特征精准捕捉并融合!

本文提出了一种双模态注意力融合模块(DAFM),用于增强多模态图像融合任务中的特征交互。该模块通过双注意力聚合机制,结合自注意力与跨模态注意力,有效提升特征融合效果。DAFM采用可学习平衡因子动态调整注意力权重,并运用深度卷积降低计算复杂度。实验结果表明,该模块能显著提升多模态图像融合质量,适用于RGB-红外等双模态视觉任务。模块代码实现简洁,可直接集成到现有网络中作为特征融合层使用。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 176

【LeetCode & 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...

【LeetCode &amp; 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列) 【LeetCode &amp; 剑指offer 刷题笔记】目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that...

技术改变世界 1315

magic-api的鉴权机制配置 (附实战代码)

引入magic-api及相关数据库驱动依赖 配置yml文件和编辑器JS文件 实现登录鉴权逻辑,包括用户验证和Token处理 接口鉴权拦截器设计,确保接口访问安全性 文章提供了完整的代码示例,帮助开发者快速集成magic-api框架并实现权限控制功能。

码农研究僧的博客 227

【AI for Medecine】吴恩达医学图像AI专项课程(诊断、预测、治疗)

近期需要简单了解AI4Science上的一些应用,看到吴恩达大佬的专项课程,于是开始来个“全面”的了解--医学人工智能(诊断、预测、治疗)。

图挖掘领域,新晋砖家 ☞ 未来可期,欢迎和静静一起学习交流吖 774

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

QCC304x系列开发教程(实战篇)之5.2 QCC3040之提示音

earbud_init.c中,初始化table中UiPrompts_Init /*! \brief Table of initialisation functions */ static const init_table_entry_t appInitTable[] = { ......... {UiPrompts_Init, 0, NULL}, ......... } ui_prompts.c中是提示音音处理的主要逻辑和配置源文件 函数static void uiPro...

心跳包的博客 2393

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

dolphin_wby的博客 283

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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 to right. The first integer of each...

zero 162

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

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

henryzhihua 2247

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

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 intege

onlyou2030的博客 408

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 i...

qq_34314736的博客 152

(java)leetcode74: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 left to right.The fir

444

Leetcode: 74. 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 each row are sorted from left to right. The first int...

anrui0096的博客 141

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 to right. The first integer of each row...

卷卷萌的博客 316

LeetCode刷题:74. Search a 2D Matrix

LeetCode刷题:74. Search a 2D Matrix 原题链接:https://leetcode.com/problems/search-a-2d-matrix/ Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following prop...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 233

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 ro...

博客 182

LeetCode 74: Search A 2D Matrix

Difficulty: 3 Frequency: 3 Problem: 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

claviclelau的专栏 443
上一篇: 二分查找-BinarySearch
下一篇: leetcode 240 : Search a 2D Matrix II
HelloCooper
博客等级 码龄11年 13粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值