当前位置:网站首页>74. 搜索二维矩阵
74. 搜索二维矩阵
2022-07-21 05:15:00 【安吉_lh1029】
1、题目描述
2、题目分析:
- 每行中的整数从左到右按升序排列。
- 每行的第一个整数大于前一行的最后一个整数。
2-1、【暴力法】整理题目思路
1、可以知道坐标在[0][0]点的元素,一定是最小元素,如果等于matrix[0][0] , 则返回true, 如果(target < matrix[0][0]), 即比矩阵中最小值还小, 则返回false
2、接下来遍历每一行第一个元素,即matrix[i][0], 如果等于某一行第一个元素,返回true, 如果matrix[i][0] > target, 则终止此次循环,跳出。
3、此时可以得到目标行下标
4、同理在对应行遍历每一列元素,如果有相等的则返回true, 否则为false.
这个逻辑比较好想,但是没啥巧妙之处,实现代码如下:
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0)
return false;
int row = matrix.length;
int col = matrix[0].length;
//初始校验
int minNum = matrix[0][0];
if(target == minNum) return true;
else if(target < minNum) return false;
int aimRow = 1;
//遍历每一行第一个元素,确定目标值在哪一行
for(; aimRow < row; aimRow++){
int rowNum = matrix[aimRow][0];
if(rowNum == target) return true;
else if (rowNum > target) break;
}
//此时确定了所在行的下标,比所在行的最大值还大,则也不存在目标值
aimRow--;
if(target > matrix[aimRow][col-1])
return false;
//遍历目标行的每一列
for(int j = 1; j < col; j++){
int colNum = matrix[aimRow][j];
if(colNum == target) return true;
}
return false;
}
}
复杂度分析
时间复杂度:O(n+m), 其中 m 和 n 分别是矩阵的行数和列数。
空间复杂度:O(1)。
2-2、针对以上思路,进行代码优化
上面题目的解题思路其实就是坐标轴并进行比较大小逻辑。同时进行,【行】+【列】遍历.
根据题意已知,二维数组从左往右递增,从上往下递增,所以得出以下结论:
某列的某个数字,该数之上的数字,都比其小;
某行的某个数字,该数右侧的数字,都比其大;
所以,解题流程如下所示:
以二维数组左下角为原点,建立直角坐标轴。
若当前数字大于了查找数,查找往上移一位。
若当前数字小于了查找数,查找往右移一位。
代码写法如下:
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
//从【左下角】开始遍历,如果target < 当前值 ,rows--, 如果 target>当前值,col++
int rows = matrix.length - 1, columns = 0;
while (rows >= 0 && columns < matrix[0].length) {
int num = matrix[rows][columns];
if (num == target) {
return true;
} else if (num > target) {
rows--;
} else {
columns++;
}
}
return false;
}
}
复杂度分析
时间复杂度:O(n+m), 其中 m 和 n 分别是矩阵的行数和列数。
空间复杂度:O(1)。
其他:还可以根据官方思路,进行二分查找等思路。
边栏推荐
- 深入剖析斐波拉契数列
- robotframework 常用快捷键
- 使用uuid做MySQL主键,被老板,爆怼了一顿
- It turns out that someone plays vscode into idea, which is a little powerful
- 19. 删除链表的倒数第 N 个结点
- openGl新手入门学习笔记(一)什么是openGl,使用glfw库和环境搭建
- What impact does the Internet of things have on the development of enterprises?
- Learning notes for beginners of OpenGL (I) what is OpenGL, using glfw library and environment construction
- 543. 二叉树的直径
- Introduction to general process choreography engine
猜你喜欢
Appium+pytest+allure realizes app automated testing, which is a small test
Good looking and interesting data visualization chart making, worship tutorial
The importance of PLC industrial control board development
Google Chrome -- xpathhelper installation
What impact does the Internet of things have on the development of enterprises?
一起学习gcc gdb makefile
Heap - principle to application - heap sort, priority queue
22张图带你深入剖析前缀、中缀、后缀表达式以及表达式求值
01 knapsack interview questions series (I)
Qt5 GUI
随机推荐
Summary of Niuke online question brushing -- top101 must be brushed for interview
Special answer: what does NMN concept stock mean and what is NMN in the end
Mstest cannot exit
面试官:你的update 更新影响行数做判断了吗?
The importance of PLC industrial control board development
手把手教你在服务器上用YOLOv4训练和测试数据集(保姆级)
Source Insight 4.0个性化设置及快捷键
你真的懂01背包问题吗?01背包的这几问你能答出来吗?
YOLOv4(darknet版)后处理:显示置信度、保存检测框的内容到本地
What is the gateway? What is the Internet of things gateway?
西瓜书第三章-线性模型
Summary of actual process and steps of interface test
Introduction to general process choreography engine
04-unittest單元測試框架
一起学习多线程、进程、开发板
Liteos connector script (2)
Quartz. Net c tutorial - course 6: crontrigger
用两个模板(递归法和迭代法)完成二叉树的四种遍历方式
重磅:国产IDE发布,由阿里研发,完全开源了(高性能+高定制性)
干货 | 分布式系统的数据分片难题