LeetCode OJ 240. Search a 2D Matrix II

告别SMB共享烦恼:基于P2P直连的零配置远程文件互传方案 文件传输是计算机数据交换的基础需求,其核心在于如何在网络节点间高效、安全地移动数据。传统方案如SMB协议依赖复杂的网络配置和系统共享设置,尤其在跨网络、跨设备场景下,面临NAT穿透、防火墙规则和公网暴露等挑战。现代P2P(点对点)技术通过应用层直接建立加密通信通道,绕开了底层网络复杂性,实现了零配置的直连传输,其技术价值在于兼顾了易用性、传输效率与安全性。这种方案特别适用于需要频繁进行大文件交换的远程协作、跨平台设备互传以及家庭与办公环境间的数据同步等场景。本文以实际工具为例,深入解析了如何利用P2P直连与 阅读详情

LeetCode OJ 240. Search a 2D Matrix II


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 in ascending from left to right.

  • Integers in each column are sorted in ascending from top to bottom.

For example,

Consider the following 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]
]

Given target = 5, return true.

Given target = 20, return false.

每一行采用二分查找(Binary Search)的方法。对于nxn的矩阵来说,每一行时间复杂度为O(logn),一共n行,总时间复杂度是O(nlogn)。

代码


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

            int low, high, mid;
            for(int i = 0; i < m; i++){
                low = 0; high = n - 1;
                while(low <= high){
                    mid = low + (high - low) / 2;
                    if(target == matrix[i][mid])
                        return true;
                    else if(target > matrix[i][mid])
                        low = mid + 1;
                    else
                        high = mid - 1;
                }
            }
        return false;
    }
};

方法二:Divide and Conquer

因为矩阵中的数每一行每一列都是非递减序列,所以,我们可以采用分治的思想,从右上角开始,每次去除一行或者一列,不断缩小范围。eg.
题目给出的例子矩阵,

[
[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]
]

假设我们查找的数为8,那么从右上角15判断起,8小于15和11,每一行每一列都是非递减序列,所以肯定不在这两列。此时,我们查找范围变成:

[
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[10, 13, 14],
[18, 21, 23]
]

又8大于7,所以8不在7所在的行,此时,范围变为:

[
[2, 5, 8],
[3, 6, 9],
[10, 13, 14],
[18, 21, 23]
]

右上角是8,则返回true。
如果查找的下标i,j超出了矩阵范围,则表示不存在这个数,返回false。
算法最坏的情况是从矩阵右上角每次向左或向下遍历一个元素,最后到左下角,因此算法时间复杂度为O(n)。

代码

个人github代码链接


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

        int i = 0, j = n - 1;
        while(i < m && j >= 0){
            if(target == matrix[i][j])
                return true;
            else if(target > matrix[i][j])
                i ++;
            else 
                j --;
        }
        return false;
    }
};
matrix calculus 讲述矩阵运算 求导。 Matrix Calculus - Notes on the Derivative of a Trace Johannes Traa 立即下载

相关推荐

The matrix cookbook-matrix calculus 矩阵运算讲义

矩阵入门必读,非常详细,内容包括矩阵的各种运算技巧,可以当做工具性的参考书,随查随用

Matrix calculus 矩阵微分

转载地址:https://en.wikipedia.org/wiki/Matrix_calculus 前提: 若 x为向量,则默认 x为列向量, xT 为行向量,后面提到的两种布局都是这样认为的。 Types of Matrix Derivatives Types Scalar Vector Matrix Scalar Vector ...

子墨的专栏 4513

GNSS接收机性能测试指标详解:TTFF、位置精度、灵敏度等关键参数如何评估

本文详细解析了GNSS接收机性能测试的关键指标,包括首次定位时间(TTFF)、位置精度、灵敏度等,帮助工程师系统评估接收机性能。通过多频段协同定位和载波相位测试等技术,提升定位精度至亚米级。同时,介绍了灵敏度测试的极限挑战和授时精度的专业测试方案,为GNSS接收机的性能优化提供实用指导。

cuda7parallel的博客 978

求导四则运算以及三角函数求导 Derivative formulas

对特定函数的求导。 1:sin(x) 对其进行求斜率。带入公式得:[ sin(x+Δx)- sin(x)]/Δx = [ sinx*cosΔx + cosx*sinΔx -sin x ]/ Δx = [ cos x * sin Δx ] / Δx = cos x     cos Δx = 1 当Δx无限趋近于0 的时候 ,sin x和x在 0点是一样的,其取值也一样。 2:co...

weixin_34221036的博客 509

Android Studio运行报错: failed to load include path ……\platforms\android-35\android.jar的最细解决方案

【代码】Android Studio运行报错: failed to load include path ……\platforms\android-35\android.jar的最细解决方案。

2401_86789230的博客 2万+

最全卷积总结篇(分组、深度可分离、空洞、转置等)

分组卷积(Group Convolution)是一种卷积操作方式,它最早在AlexNet中引入,并在深度学习中被广泛使用,尤其是在高效模型设计(如MobileNet和ResNeXt)中。分组卷积通过将输入特征通道分为若干组,并在每组内单独进行卷积操作,可以减少计算复杂度和参数量,同时有效利用模型的结构特性。分组卷积的工作原理通道分组:输入特征图的通道图为C_in,输出特征图的通道数为C_out,将每组的C_in和C_out分为G组,每组的通道数为C_in/G和C_out/G。组内卷积。

qq_44815135的博客 4234

leetcode oj java Search a 2D Matrix II

一、问题描述: 在剩下的里边找最右上角的,是11. 11> 5 因此应该从11的左边找(即删掉11所在的列); Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in eac

Xcy的博客 266

【WRF模拟】土地利用类型的确定:最大覆盖法/瓦片法

土地利用类型的确定:最大覆盖法/瓦片法

WW、forever的博客 1535

LeetCode OJ 之 Search a 2D Matrix II (二维矩阵查找)

题目: 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 in ascending from left to right.Int

灯火阑珊处 613

微信小程序 - 强力解决播放视频卡顿问题,小程序使用video组件播放视频或第三方视频插件,会出现卡顿或者无法播放的各种奇怪问题(微信小程序苹果ios/安卓播放视频很卡不流畅、视频太长或文件大等情况)

微信小程序视频组件播放卡顿,微信小程序video组件出现无法播放或卡顿,微信小程序video组件播放时会出现卡顿,微信小程序使用video组件播放视频的时候,会出现卡顿或者无法播放的问题,微信小程序安卓视频播放卡顿问题,微信小程序苹果ios手机播放视频不流畅很卡,加cdn还是卡,uniapp播放视频会有明显的卡顿问题,video视频时卡顿不流畅问题,为什么video组件播放视频卡顿?苹果微信小程序通过video组件播放视频时卡顿,怎么解决?微信小程序video组件出现无法播放或卡顿,设置custom-cac

🏆 前端领域优质创作者 1万+

AUTOSAR进阶图解==>AUTOSAR_SWS_COM

AUTOSAR COM模块是AUTOSAR通信堆栈的核心组件,提供信号导向的通信服务。该模块位于通信服务层,连接RTE与PDU Router,主要功能包括信号封装/提取、传输模式管理、信号路由、超时监控和字节序转换等。COM模块架构分为应用层、通信服务层和通信抽象层,包含信号管理、I-PDU处理、传输模式管理等组件。其配置结构包括顶层容器、I-PDU组、I-PDU和信号等多层次配置对象,支持丰富的参数设置。模块采用严格的状态管理机制,包括模块状态和I-PDU组状态转换,确保通信可靠性。

一名留德华的,康德的信徒的,专注于汽车电子领域的博客 1254

LeetCode OJ 451. Sort Characters By Frequency

LeetCode OJ 451. Sort Characters By FrequencyDescriptionGiven a string, sort it in decreasing order based on the frequency of characters.Example1 Input: “tree” Output: “eert” Explanat

jan_deng的博客 507

【UVM实战】第一章:配置UVM环境(以VCS为例),跑通书上的例子(包括清理中间文件方法、使用DVE查看波形方法、命令执行原理探究)

如何配置UVM的VCS运行环境,运行书本上的例子呢?本节将会为你详细解答!

悟已往之不谏 知来者之可追 8152

LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal

LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder TraversalDescriptionGiven inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do n

jan_deng的博客 488

2025华为OD面试手撕代码真题目录、八股文真题目录以及面试技巧

1. **华为OD面试技术面准备指南** 2. **华为OD面试技术面常见问题解答** 3. **华为OD面试技术面经验分享** 4. **华为OD面试技术面注意事项** 5. **华为OD面试技术面攻略** 6. **华为OD面试技术面模拟题目与解析** 7. **华为OD面试技术面知识点梳理** 8. **华为OD面试技术面常考**

算法大师! 3万+

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

LeetCode OJ 12. Integer to Roman

LeetCode OJ 513. Find Bottom Left Tree ValueDescriptionGiven an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.解题思路整数转换为罗马数字,用一个二维数组保存1-9,10-90,100-900

jan_deng的博客 441

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode OJ 515. Find Largest Value in Each Tree Row

LeetCode OJ 515. Find Largest Value in Each Tree RowDescriptionYou need to find the largest value in each row of a binary tree.Example Input: 1 / \ 3 2 / \ \ 5 3 9 O

jan_deng的博客 432

OV9734 DATASHEET,完整规格书

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

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

LeetCode OJ 207. Course Schedule

LeetCode OJ 207. Course Schedule典型的拓扑排序算法。谨以此题纪念期末机考时没AC这么简单的拓扑排序。upset……ingDescriptionThere are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for

jan_deng的博客 373

LeetCode OJ 13. Roman to Integer

LeetCode OJ 13. Roman to IntegerDescriptionGiven a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.解题思路罗马数字转换为整数:我们把1,10,100,1000,5,500,500和对应的罗马数字做映射。

jan_deng的博客 356

LeetCode OJ 566. Reshape the Matrix

LeetCode OJ 566. Reshape the MatrixDescriptionIn MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’r

jan_deng的博客 332

LeetCode OJ 155. Min Stack

LeetCode OJ 155. Min StackDescriptionDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element o

jan_deng的博客 330

LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal

LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder TraversalDescriptionGiven preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not

jan_deng的博客 324

LeetCode OJ 496. Next Greater Element I

LeetCode OJ 513. Find Bottom Left Tree ValueDescriptionYou are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nu

jan_deng的博客 313

LeetCode OJ 141. Linked List Cycle

LeetCode OJ 141. Linked List CycleDescriptionGiven a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?解题思路对于判断环的问题,可以采用双指针(快慢指针)的方法来判断。另外,因为题目中给的Lis

jan_deng的博客 308

LeetCode OJ 399. Evaluate Division

LeetCode OJ 399. Evaluate DivisionDescriptionEquations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some

jan_deng的博客 300
上一篇: LeetCode OJ 442. Find All Duplicates in an Array
下一篇: LeetCode OJ 56. Merge Intervals
jan_deng
博客等级 码龄11年 1粉丝 26原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值