当前位置:网站首页>leetcode:240. 搜索二维矩阵 II
leetcode:240. 搜索二维矩阵 II
2022-07-15 16:25:00 【OceanStar的学习笔记】
题目解析
题目描述


class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
}
};
题目解析
分析题意
就是说,有一个行递增,列也递增的矩阵,在里面找一个target
思路
既然是有序数组,那我们就要好好利用它的单调性,比如说二分之类的
在m x n矩阵 matrix中我们可以发现一个性质:对于每个子矩阵右上角的数x,x左边的数都小于等于x,x下边的数都大于x,如下图所示:

因此我们可以从整个矩阵的右上角开始枚举,假设当前枚举的数是 x:
- 如果 x 等于target,则说明我们找到了目标值,返回true;
- 如果 x小于target,则 x左边的数一定都小于target,我们可以直接排除当前一整行的数;
- 如果 x 大于target,则 x 下边的数一定都大于target,我们可以直接排除当前一整列的数;
排除一整行就是让枚举的点的横坐标加一,排除一整列就是让纵坐标减一。当我们排除完整个矩阵后仍没有找到目标值时,就说明目标值不存在,返回false。

具体过程如下:
- 1、初始化i = 0, j = matrix[0].size() - 1。
- 2、如果matrix[i][j] == target,返回true。
- 3、如果matrix[i][j] < target,i++,排除一行。
- 4、如果matrix[i][j] > target,j–,排除一列。
- 5、如果出界了还未找到target,则返回false。

思路二
我们可以将二维矩阵抽象成【以右上角为根的BST】

那么我们可以从根(右上角)开始搜索,如果当前节点不等于目标值,可以按照树的搜索顺序进行:
- 当前节点[大于]目标值,搜索当前节点的[左子树],也就是当前矩阵位置的「左方格子」,即j–
- 当前节点「小于」目标值,搜索当前节点的「右子树」,也就是当前矩阵位置的「下方格子」,即i++
实现
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.empty()){
return false;
}
int m = matrix.size(), n = matrix[0].size();
int i = 0, j = n - 1;
while (i >= 0 && i < m && j >= 0 && j < n){
int curr = matrix[i][j];
if(curr == target){
return true;
}else if(curr > target){
--j;
}else{
++i;
}
}
return false;
}
};

边栏推荐
- 个人买股票应该使用什么app更安全,交易速度更快
- Matplotlib绘图报错:“! LaTeX Error: File `type1cm.sty‘ not found.“ 解决办法
- New usage and deconstruction assignment of data types
- bisect模块
- Face the object
- 函数与箭头函数
- Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (top)
- 10 database optimization best practices for web developers
- Loj#576-「LibreOJ NOI Round #2」签到游戏【线段树】
- [MySQL learning notes 33] log
猜你喜欢
Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (top)

let / const /var的区别

What is the real HTAP? (1) Background article

Practice of online problem feedback module (III): automatically generate all controller, service, mapper and other files

997. Find the judge of the town

Matlab-mex

在线问题反馈模块实战(二):封装代码自动生成类文件器

Intensive reading of the paper: deep reinforcement learning and intelligent transportation (I)

从数字化到智能运维:有哪些价值,又有哪些挑战?

正则表达式
随机推荐
2022年低压电工考试题模拟考试平台操作
函数高级应用
regular expression
Diwen serial port screen tutorial (1)
Together with Alibaba cloud, grafana labs will provide the first grafana hosting service in China
A new UI testing method: visual perception test
997. 找到小镇的法官
杰理之本无法搜索到蓝牙解决方法【篇】
flink. 14. How is the underlying layer of datastream module source implemented?
Jerry's use of one driven two burner burning notes [chapter]
[deep learning] the speed accuracy of yolov7 exceeds that of other variants. Great God AB tweeted, netizen: it has to be you| Open source
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置position参数配置不同分组数据点的分离并且是抖动数据点
2、Deep Learning in Higher Dimensions
bisect模块
997. Find the judge of the town
ECCV 2022 | multi domain long tail distributed learning, research on unbalanced domain generalization (open source)
【golang】基于go语言的堆结构模板
Jerry's n turns on the 1-2 function, which will affect the Bluetooth distance [article]
蓝桥杯2022年第十三届省赛第三题-求和 (前缀和 或 公式法)
干货!手把手教你搭建高可用架构