当前位置:网站首页>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;
}
};
边栏推荐
- Codeforces Round #392 (Div. 2) D. Ability To Convert
- Target of cmake command_ compile_ options
- Handling method of Oracle data file header SCN inconsistency
- leetcode--字符串
- Support vector machine (SVC, nusvc, linearsvc)
- 零基础自学SQL课程 | 相关子查询
- Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
- The ambition of JD instant retailing from 618
- NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读
- Implementation of simple floating frame in WindowManager
猜你喜欢
Servlet快速筑基
Ggplot2 color setting summary
Prct-1400: failed to execute getcrshome resolution
The ambition of JD instant retailing from 618
Why is LNX of e equal to X
零基础自学SQL课程 | HAVING子句
Inspiration from reading CVPR 2022 target detection paper
ApplicationContextInitializer的三种使用方法
Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?
NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读
随机推荐
【自定义Endpoint 及实现原理】
Conseils étonnants pour promouvoir les ventes d'entreprise avec le chat en direct
R 椭圆随机点产生并画图
Amazing tips for using live chat to drive business sales
牛客网 十进制整数转十六进制字符串
L01_一条SQL查询语句是如何执行的?
Project deployment related
使用Live Chat促进业务销售的惊人技巧
[custom endpoint and implementation principle]
Oracle database expdp only exports tables
Vidéo courte recommandée chaque semaine: Soyez sérieux en parlant de "métaunivers"
How to make social media the driving force of cross-border e-commerce? This independent station tool cannot be missed!
NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读
Ggplot2 color setting summary
CF566E-Restoring Map【bitset】
Niuke.com string deformation
正则匹配邮箱
20、 Processor scheduling (RR time slice rotation, mlfq multi-level feedback queue, CFS fully fair scheduler, priority reversal; multiprocessor scheduling)
Oracle数据库监听文件配置
开源一款监控数据采集器,啥都能监控