当前位置:网站首页>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;
}
};
边栏推荐
- Literature Research Report
- 如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!
- [Eureka registry]
- 医学图像开源数据集汇总(二)
- tp5 使用post接收数组数据时报variable type error: array错误的解决方法
- 转:三星电子CEO:一切决策都要从认清自己开始
- 活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
- Talking about the knowledge of digital transformation
- Weekly recommended short video: is the ultimate form of computing "meta universe"?
- Turn to: CEO of Samsung Electronics: all decisions should start from recognizing yourself
猜你喜欢

Some common pitfalls in getting started with jupyter:

5分钟,客服聊天处理技巧,炉火纯青

谈谈数字化转型晓知识

Oracle 12c升级至19c后ORA-28000错误

June 13-19, 2022 AI industry weekly (issue 102): career development

最新Windows下Go语言开发环境搭建+GoLand配置

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

Xiaobai needs to learn MySQL - incremental statistical SQL

Learn Tai Chi Maker - esp8226 (12) esp8266 multitasking

使用Live Chat促进业务销售的惊人技巧
随机推荐
leetcode--字符串
PHP封装一个文件上传类(支持单文件多文件上传)
Zero foundation self-study SQL course | sub query
Reasons for the failure of digital transformation and the way to success
Why is LNX of e equal to X
百度AI模板 获取知识理解
P6698-[BalticOI 2020 Day2]病毒【AC自动机,dp,SPFA】
PTA猴子选大王(约瑟夫环问题)
【自定义Endpoint 及实现原理】
算法--找到和最大的长度为 K 的子序列(Kotlin)
活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
获取带参数的微信小程序二维码-以及修改二维码LOGO源码分享
零基础自学SQL课程 | 相关子查询
牛客网 十进制整数转十六进制字符串
Turn to: CEO of Samsung Electronics: all decisions should start from recognizing yourself
Cdga | how can we do well in data governance?
Nlp-d59-nlp game D28 - I think it's OK - stage summary - mentality adjustment
In depth analysis of Apache bookkeeper series: Part 3 - reading principle
Leetcode-- string
Handling method of Oracle data file header SCN inconsistency