当前位置:网站首页>LeetCode: 240. Search 2D matrix II
LeetCode: 240. Search 2D matrix II
2022-06-24 09:40:00 【Whisper_ yl】
Write an efficient algorithm to search m x n matrix matrix One of the goals in target . The matrix has the following characteristics :
The elements of each row are arranged in ascending order from left to right .
The elements of each column are arranged in ascending order from top to bottom .
Example 1:

Input :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
Output :true
Example 2:

Input :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 = 20
Output :false
Tips :
m == matrix.length
n == matrix[i].length
1 <= n, m <= 300
-109 <= matix[i][j] <= 109
All elements of each row are arranged in ascending order from left to right
All elements in each column are arranged in ascending order from top to bottom
-109 <= target <= 109
analysis :
Each row and column of the matrix are arranged in ascending order , The elements in the lower left and upper right corners of the matrix are very special , Take the upper right element as an example : All numbers to the left of the element in this line are smaller than it , All the numbers below this element in this column are larger than it . We can use this property to reduce the search space . set up row = 0,column = matrix[0].size() - 1, take matrix[row][column] And target Contrast . If matrix[row][column] > target, Then we can exclude all the elements in this column , Because it is already the smallest element in the column , therefore column--; If matrix[row][column] < target, So we can exclude all the elements in this line , Because it is already the largest element in the row , therefore row++. By changing row and column, We can effectively reduce the search space , Increase of efficiency . Personally, I think there is a very good explanation , The essence of double pointer is to use the properties of the topic , Exclude some combinations that do not contain correct answers . Worthy of aftertaste .
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = 0, column = matrix[0].size() - 1;
while(row < matrix.size() && column >= 0){
if(matrix[row][column] < target)
row++;
else if(matrix[row][column] > target)
column--;
else
return true;
}
return false;
}
};
边栏推荐
- [GDB debugging tool] | how to debug under multithreading, multiprocessing and running programs
- 从618看京东即时零售的野心
- Zero foundation self-study SQL course | sub query
- Ggplot2 color setting summary
- Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
- [bug] @jsonformat has a problem that the date is less than one day when it is used
- Weekly recommended short video: is the ultimate form of computing "meta universe"?
- How to locate lock waiting in Dameng database
- Endgame P.O.O
- Amazing tips for using live chat to drive business sales
猜你喜欢

Support vector machine (SVC, nusvc, linearsvc)

Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?

e的lnx为什么等于x

如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!

Xiaobai needs to learn MySQL - incremental statistical SQL

Time Series Data Augmentation for Deep Learning: A Survey 之论文阅读

字节跳动-面试官: 谈下音视频同步原理,音频和视频能绝对同步吗?

CDGA|到底怎么才能做好数据治理呢?

ggplot2颜色设置总结

How to make social media the driving force of cross-border e-commerce? This independent station tool cannot be missed!
随机推荐
实战剖析:app扫码登陆实现原理(app+网页端详细逻辑)附源码
Seekbar with text: customize progressdrawable/thumb: solve incomplete display
Grpc local test joint debugging tool bloomrpc
Inspiration from reading CVPR 2022 target detection paper
Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
【bug】@JsonFormat 使用时出现日期少一天的问题
Threejs point light + ambient light
Threejs MMD model loading + contour loading + animation loading + Audio loading + camera animation loading +ammojs loading gltf model loading +gltf reflection adjustment
深入解析 Apache BookKeeper 系列:第三篇——读取原理
R 椭圆随机点产生并画图
算法--找到和最大的长度为 K 的子序列(Kotlin)
When programmers are asked if they can repair computers... | daily anecdotes
《MATLAB 神经网络43个案例分析》:第32章 小波神经网络的时间序列预测——短时交通流量预测
PHP使用递归和非递归方式实现创建多级文件夹
[Eureka source code analysis]
如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!
软件系统依赖关系分析
Some common pitfalls in getting started with jupyter:
How to locate lock waiting in Dameng database
WindowManager 简单悬浮框的实现