当前位置:网站首页>LeetCode 74、搜索二维矩阵
LeetCode 74、搜索二维矩阵
2022-04-23 20:23:00 【亡于灬】
74、搜索二维矩阵
1)题目描述
编写一个高效的算法来判断 m x n
矩阵中,是否存在一个目标值。该矩阵具有如下特性:
- 每行中的整数从左到右按升序排列。
- 每行的第一个整数大于前一行的最后一个整数。
示例 1:
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
输出:true
示例 2:
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
输出:false
提示:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 100
-10^4 <= matrix[i][j], target <= 10^4
2)分析
行遍历,行内二分。
- 由题可知,按照行先遍历矩阵,矩阵元素是非递减的。那么如果目标值小于矩阵行先遍历第一个元素或者大于最后一个元素,目标值一定不再矩阵中;
- 若目标值可能在矩阵中,则遍历矩阵每一行,将目标值与当前行的最后一个元素比较,判断目标值是否可能在当前行中;
- 若目标值在当前行中,通过二分法搜索对应的元素的下标;
- 判断搜索到的下标对应的元素与目标值是否相等。
3)C++
代码
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix[0][0]>target||matrix[matrix.size()-1][matrix[0].size()-1]<target)
return false;
int m=matrix.size();
int n=matrix[0].size();
for(int i=0;i<m;i++){
if(matrix[i][n-1]<target)
continue;
else{
int left=0;
int right=n-1;
while(left<=right){
int mid=(left+right)/2;
if(matrix[i][mid]<target)
left=mid+1;
else
right=mid-1;
}
return matrix[i][left]==target;
}
}
return false;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124360319
边栏推荐
- Es index (document name) fuzzy query method (database name fuzzy query method)
- What is the difference between a host and a server?
- redis 分布式锁
- JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
- 16MySQL之DCL 中 COMMIT和ROllBACK
- 【PTA】L1-006 连续因子
- Some basic configurations in interlij idea
- How does onlyoffice solve no route to host
- 【问题解决】‘ascii‘ codec can‘t encode characters in position xx-xx: ordinal not in range(128)
- Cadence Orcad Capture 批量更改元件封装功能介绍图文教程及视频演示
猜你喜欢
Plato Farm元宇宙IEO上线四大,链上交易颇高
Redis cache penetration, cache breakdown, cache avalanche
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
Tensorflow 2 basic operation dictionary
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
DNS cloud school rising posture! Three advanced uses of authoritative DNS
Some basic configurations in interlij idea
随机推荐
The R language uses the timeroc package to calculate the multi time AUC value of survival data without competitive risk, and uses the confint function to calculate the confidence interval value of mul
JDBC database addition, deletion, query and modification tool class
R language survival package coxph function to build Cox regression model, ggrisk package ggrisk function and two_ Scatter function visualizes the risk score map of Cox regression, interprets the risk
Notes of Tang Shu's grammar class in postgraduate entrance examination English
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
Unity 模型整体更改材质
Modeling based on catiav6
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
Recognition of high-speed road signs by Matlab using alexnet
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
如何做产品创新?——产品创新方法论探索一
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
Local call feign interface message 404
Markdown < a > tag new page open link
Is the wechat CICC wealth high-end zone safe? How to open an account for securities
Remote code execution in Win 11 using wpad / PAC and JScript 3
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
Tensorflow 2 basic operation dictionary
【PTA】L1-002 打印沙漏