当前位置:网站首页>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;
}
};
边栏推荐
- ThinkPHP 5.0 模型关联详解
- LeetCode: 137. 只出现一次的数字 II
- Algorithm -- find and maximum length k subsequence (kotlin)
- An open source monitoring data collector that can monitor everything
- Go language project development practice directory
- Ggplot2 color setting summary
- 【自定义Endpoint 及实现原理】
- 可直接套用的Go编码规范
- 【Eureka 源码分析】
- 数字化转型的失败原因及成功之道
猜你喜欢

Cdga | how can we do well in data governance?

ApplicationContextInitializer的三种使用方法

零基础自学SQL课程 | 相关子查询

Numpy NP in numpy c_ And np r_ Explain in detail

Reasons for the failure of digital transformation and the way to success

R ellipse random point generation and drawing

Oracle数据文件头SCN不一致处理方法

impdp导schema报ORA-31625异常处理

Zero foundation self-study SQL course | having clause

PhpStrom代码格式化设置
随机推荐
When programmers are asked if they can repair computers... | daily anecdotes
Amazing tips for using live chat to drive business sales
《MATLAB 神经网络43个案例分析》:第32章 小波神经网络的时间序列预测——短时交通流量预测
ggplot2颜色设置总结
P6117-[JOI 2019 Final]コイン集め【贪心】
Handling method of Oracle data file header SCN inconsistency
Turn to: CEO of Samsung Electronics: all decisions should start from recognizing yourself
LeetCode: 377. 组合总和 Ⅳ
牛客网 实现简单计算器功能
算法--找到和最大的长度为 K 的子序列(Kotlin)
[Eureka registry]
Oracle database listening file configuration
Niuke.com string deformation
Zero foundation self-study SQL course | related sub query
Talking about the knowledge of digital transformation
百度AI模板 获取知识理解
最新Windows下Go语言开发环境搭建+GoLand配置
CDGA|到底怎么才能做好数据治理呢?
Cmake命令之target_compile_options
软件系统依赖关系分析