Leetcode 74. Search a 2D Matrix

RT-Thread网络编程:基于STM32F407的TCP客户端实现(含完整代码解析) 本文详细介绍了在STM32F407平台上,基于RT-Thread操作系统和LWIP协议栈实现工业级可靠TCP客户端的完整过程。内容涵盖底层ETH驱动配置、Socket编程、连接保活与断线重连机制、数据收发协议设计以及资源管理,提供了可应用于实际项目的健壮代码框架与调试技巧。 阅读详情

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.

题目链接:https://leetcode.com/problems/search-a-2d-matrix/description/
题目大意:在一个n * m的矩阵中查找目标数字target,找到返回true,否则返回false。矩阵满足以下条件:
1. 每行升序有序。
2. 每行第一个数大于上一行的最后一个数。
解题思路:因为矩阵的行和列有序性,可以用二分查找的方法把时间复杂度降到 log(nm) ,先二分查到所在的行,用时log(n),再对该行二分查找,用时log(m)。
代码如下:
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 = n-1;
        int mid = (l + r)/2;
        while(l <= r){
            if(target >= matrix[mid][0] && target < matrix[mid][0] + 1){
                break;
            }else if(target > matrix[mid][0]){
                l = mid + 1;
            }
            else
                r = mid - 1;
            mid = (l + r)/2;
        }
        if(target >= matrix[n-1][0]){
            mid = n - 1;
        }
        l = 0,r = m - 1;
        int md = (l + r)/2;
        while(l <= r){
            if(target == matrix[mid][md]){
                return true;
            }else if(target > matrix[mid][md]){
                l = md + 1;
            }
            else
                r = md - 1;
            md = (l + r)/2;
        }
        return false;
    }
};


别再手动敲代码了!用STM32CubeMX+FreeRTOS图形化配置,5分钟搞定多任务通信框架 本文详细介绍了如何利用STM32CubeMX和FreeRTOS快速搭建多任务通信框架,大幅提升嵌入式开发效率。通过图形化配置工具,开发者可在5分钟内完成任务调度、时钟配置和内核对象生成,特别适合物联网设备开发。文章还提供了代码优化和调试技巧,帮助开发者轻松应对复杂应用场景。 阅读详情

相关推荐

Computer Networks 和 Computer & Security投稿经验

论文投稿

qq_50604294的博客 9225

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

R语言clusterProfiler实战:GO与KEGG富集分析从入门到精通

本文提供了一份详尽的R语言clusterProfiler实战指南,从环境配置、数据导入、基因ID转换到GO与KEGG富集分析及可视化,手把手教你解读生物学意义。文章重点介绍了如何利用clusterProfiler这一强大工具,将差异基因列表转化为清晰的生物学通路与功能故事,并分享了结果解读、可视化以及处理非模式生物等进阶技巧与避坑指南。

weixin_29044713的博客 162

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

AI辅助专利撰写实战:30分钟高效产出发明专利初稿

在技术创新的浪潮中,专利撰写常常是开发者将创意转化为知识产权保护壁垒的关键一步,但这个过程往往耗时费力,需要兼顾技术深度与法律文书规范。近期,借助AI辅助工具来提升专利撰写效率已成为一个热门探索方向。本文将围绕如何利用AI工具,在30分钟内高效完成一篇具备授权潜力的发明专利初稿,进行一场从0到1的实战演练。无论你是独立开发者、初创团队的技术负责人,还是希望保护自己研究成果的学生,这套方法都能帮助你快速梳理技术方案,形成结构严谨、逻辑清晰的专利申请文档。 1. 背景与核心概念:AI辅助专利撰写的价值与边界 在

weixin_34253126的博客 490

手把手教你用DaVinci Configurator配置AUTOSAR SWC接口(含RTE避坑指南)

本文提供了一份基于DaVinci Configurator工具的AUTOSAR软件组件(SWC)接口配置实战指南。文章详细拆解了SR(发送者-接收者)和CS(客户端-服务器)接口的配置流程,并重点分享了运行时环境(RTE)生成阶段在多核部署、数据类型匹配等常见陷阱中的避坑方案与调试技巧,旨在帮助汽车电子工程师高效完成配置并规避错误。

uuu88的博客 547

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

HFSS线圈仿真避坑指南:从建模到寄生参数分析的完整流程

本文详细介绍了HFSS线圈仿真的完整流程,从精准建模到寄生参数优化,特别针对Ansys Electronics Desktop用户提供实用指南。涵盖线圈几何构建、材料属性配置、端口激励设置及寄生电阻与电感分析等关键步骤,帮助工程师避免常见错误,提升仿真精度与效率。

weixin_29209801的博客 548

LeetCode74——Search a 2D Matrix

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

上善若水 1432

使用Captum库对CIFAR数据集进行模型可解释性分析

为了训练模型,我们需要指定损失函数和优化技术。损失函数用于衡量模型的性能,在训练过程中,我们通过调整模型参数来最小化损失。本教程使用。

2501_90323865的博客 177

智能车图像处理9-进阶篇1--中线的提取

(1)10-50行 基础扫线 (2)56-71 断行0 ,就是比如遇到十字,十字路口下方 第一次断线的地方。 (3)83-124 按照原先有效行,拟线作为中值,然后从中值向左右扫线,同时计算赛道宽度。 (4)125-138 如果出现赛道宽度小于先前有效行,则找到二次扫线的起点,从二次扫线起点处,继续向下扫线,直至结束,同时记录最后的有效行。 (5) 310-- 最后计算左右边线类似于方差的值,用于环岛的判断 ***这里着重描述一下二次扫线,希望大家可以理解的深刻

taiyuezyh的博客 1万+

LeetCode 74. Search a 2D Matrix

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

六六哥的博客 777

探索gtwr of mgtwr in python

代码】探索gtwr of GWmodel in python。

m0_46268055的博客 3388

[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

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

[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

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【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

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

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

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

cannonjinx的博客 4280

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

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

imlxinyu的博客 772

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

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 45. Jump Game II(dp)
下一篇: Leetcode 91. Decode Ways (string)
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值