当前位置:网站首页>LeetCode: 240. 搜索二维矩阵 II
LeetCode: 240. 搜索二维矩阵 II
2022-06-24 08:08:00 【Whisper_yl】
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:
每行的元素从左到右升序排列。
每列的元素从上到下升序排列。
示例 1:

输入: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
输出:true
示例 2:

输入: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
输出:false
提示:
m == matrix.length
n == matrix[i].length
1 <= n, m <= 300
-109 <= matix[i][j] <= 109
每行的所有元素从左到右升序排列
每列的所有元素从上到下升序排列
-109 <= target <= 109
分析:
该矩阵每行和每列都是保持升序排列的,位于矩阵左下角和右上角的元素是很特别的,以右上角元素为例:在此行中该元素左边所有的数都比它小,在此列中该元素下边所有的数都比它大。我们可以利用该性质减少搜索空间。设row = 0,column = matrix[0].size() - 1,将matrix[row][column]与target进行对比。如果matrix[row][column] > target,那么我们可以排除这一列的所有元素,因为它已经是该列最小的元素了,所以column--;如果matrix[row][column] < target,那么我们可以排除这一行所有的元素,因为它已经是该行最大的元素了,所以row++。通过改变row和column,我们可以有效缩减搜索空间,提高效率。个人觉得有一篇题解说的很不错,双指针的本质是利用题目中所具备的性质,排除掉一部分不包含正确答案的组合。值得回味。
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;
}
};
边栏推荐
- Cmake命令之target_compile_options
- 2022.6.13-6.19 AI行业周刊(第102期):职业发展
- 零基础自学SQL课程 | 相关子查询
- 零基础自学SQL课程 | 子查询
- MYCAT read / write separation and MySQL master-slave synchronization
- How to import MDF and LDF files into MySQL workbench
- [Niuke] length of the last word of HJ1 string
- Directly applicable go coding specification
- Zero foundation self-study SQL course | related sub query
- 零基础自学SQL课程 | SQL语句语法顺序与执行顺序
猜你喜欢
![[noi Simulation Competition] send (tree DP)](/img/5b/3beb9f5fdad00b6d5dc789e88c6e98.png)
[noi Simulation Competition] send (tree DP)

【Redis实现秒杀业务①】秒杀流程概述|基本业务实现

Event registration Apache pulsar x kubesphere online meetup hot registration
![[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)](/img/b3/b156d75c7b4f03580c449f8499cd74.png)
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)

In depth analysis of Apache bookkeeper series: Part 3 - reading principle

CF566E-Restoring Map【bitset】

Xiaobai needs to learn MySQL - incremental statistical SQL

Kaformer personal notes

荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?

Linux MySQL installation
随机推荐
Dynamic saving and recovery of FPU context under risc-v architecture
2022.06.23 (traversal of lc_144,94145\
Chapter 7 operation bit and bit string (III)
零基础自学SQL课程 | 子查询
Project deployment related
cookie加密 4 rpc方法确定cookie加密
Common emoticons
学习太极创客 — ESP8226 (十三)OTA
CF566E-Restoring Map【bitset】
[noi simulation] pendulum (linear algebra, Du Jiao sieve)
What do you mean by waiting for insurance records? Where should I go for filing?
学习太极创客 — ESP8226 (十二)ESP8266 多任务处理
2021-05-20computed and watch applications and differences
牛客网 实现简单计算器功能
Numpy numpy中的np.c_和np.r_详解
怎么把mdf和ldf文件导入MySQL workbench中
L01_ How is an SQL query executed?
CF566E-Restoring Map【bitset】
Epidemic situation, unemployment, 2022, we shouted to lie down!
[Niuke] length of the last word of HJ1 string