当前位置:网站首页>LeetCode - 搜索二维矩阵
LeetCode - 搜索二维矩阵
2022-07-02 11:34:00 【小小太空人w】
日常刷题中
目录
GitHub链接
diwei00 (github.com)
https://github.com/diwei00 刷题代码都在Exercise库中提交
LeetCode链接🤨
74. 搜索二维矩阵 - 力扣(LeetCode)
https://leetcode.cn/problems/search-a-2d-matrix/submissions/
题目
编写一个高效的算法来判断
m x n矩阵中,是否存在一个目标值。该矩阵具有如下特性:
- 每行中的整数从左到右按升序排列。
- 每行的第一个整数大于前一行的最后一个整数。
示例1:
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 输出:true示例2:
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 输出:false
题目分析
由于每行中的整数从左到右按升序排列且每行的第一个整数大于前一行的最后一个整数,其实就意味着整个矩阵的数据都是升序排列的,只是分为了矩阵而已。按照这种特性我们先需要遍历矩阵中最后一列的数据,如果这一列数据比目标值target大,即在要查找数据肯定在这一行,那么只需要遍历这一行数据即可。
其实发现最后一列的数据也是升序排列的,而每一行也是升序排列的,那么意味着我们在遍历最后一列和某一行数据时可以采用二分查找的方法,但最后要查找的数是在某一行出现的,所以在遍历行时采用二分查找的方法。
函数参数分析:
一般开辟的二维数组传参都是直接将首行地址数组地址传过来,用数组指针来接收。可以看见这里的接收二维数组是一个二级指针,其实这个二维数组是在堆区用malloc开辟的。先开辟一个存地址的数组,然后在开辟很多数组,把这些数组的首地址存起来,即构成了一个二维数组。在释放时,也要先释放parr这块的数组,然后再释放pparr。(后面有代码实现)matrixSize就是矩阵的行数,matrixColSize是矩阵列数的地址,target就是要查找的目标数。
代码实现(顺序查找法)
bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target)
{
int row = matrixSize;
int col = *matrixColSize;
int i = 0;
for (i = 0; i < row; i++)
{
if (matrix[i][col - 1] > target || matrix[i][col - 1] == target)
{
break;
}
}
//for循环跳出,修正下标
if (i == row)
{
i -= 1;
}
int j = 0;
for (j = 0; j < col; j++)
{
if (matrix[i][j] == target)
{
return true;
}
}
return false;
}代码实现(二分查找法)
bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target)
{
int row = matrixSize;
int col = *matrixColSize;
int i = 0;
//找需要找目标值的行
for (i = 0; i < row; i++)
{
if (matrix[i][col - 1] > target || matrix[i][col - 1] == target)
{
break;
}
}
//for循环跳出,修正下标
if (i == row)
{
i -= 1;
}
int left = 0;
int right = col - 1;
while (left <= right)
{
int mid = (left + right) >> 1;
if (matrix[i][mid] > target)
{
right = mid - 1;
}
else if (matrix[i][mid] == target)
{
return true;
}
else
{
left = mid + 1;
}
}
//while循环跳出则找不到目标值
return false;
}代码实现(二维数组在堆区开辟)
int main()
{
//保存地址的数组
int** pa = (int**)malloc(sizeof(int) * 3);
int i = 0;
int col = 3;
//将数组地址都存pa中
for (i = 0; i < 3; i++)
{
pa[i] = (int*)malloc(sizeof(int) * col);
}
for (i = 0; i < 3; i++)
{
free(pa[i]);
}
free(pa);
return 0;
}小结
每道题目的算法都有好多种,我们要打开自己的思路,不断的去忧化自己的算法,自己也会有不一样的收获。
边栏推荐
- Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
- PTA题库 ===>复数四则运算,一帮一,考试座位号(7-73)
- Ad20 cannot select the solution of component packaging in PCB editor
- 测试框架TestNG的使用(二):testNG xml的使用
- buuctf-pwn write-ups (7)
- 途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
- Daily learning 3
- taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
- Yolov3 & yolov5 output result description
- PTA question bank== > complex four operations, one for one, examination seat number (7-73)
猜你喜欢

Fabric. JS free draw circle
![[development environment] StarUML tool (download software | StarUML installation | StarUML creation project)](/img/08/9f25515e600a3174340b2589c81b0e.jpg)
[development environment] StarUML tool (download software | StarUML installation | StarUML creation project)

Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'

Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)

Socket and socket address

Add vector formula in rich text editor (MathType for TinyMCE, visual addition)

STM32库函数进行GPIO初始化

Makefile 分隔文件名与后缀

jmeter脚本参数化

Fabric. JS zoom canvas
随机推荐
uniapp自动化测试学习
buuctf-pwn write-ups (7)
由粒子加速器产生的反中子形成的白洞
Uniapp automated test learning
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
socket(套接字)与socket地址
Obsidian installs third-party plug-ins - unable to load plug-ins
< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
Openharmony notes --------- (4)
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
4、数组指针和指针数组
String matching problem
Understanding of mongodb
Advanced C language (realize simple address book)
使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
The most complete analysis of Flink frame window function
测试框架TestNG的使用(二):testNG xml的使用
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
STM32标准固件库函数名(一)
A white hole formed by antineutrons produced by particle accelerators



