当前位置:网站首页>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;
}
小结
每道题目的算法都有好多种,我们要打开自己的思路,不断的去忧化自己的算法,自己也会有不一样的收获。
边栏推荐
- 删除元素(带过渡动画)
- Fatal: unsafe repository is owned by someone else
- Fabric.js 自由绘制椭圆
- taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
- Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
- YoloV6训练:训练自己数据集遇到的各种问题
- Method of creating linked server for cross server data access
- Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
- There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
- Why can't browsers read JSX?
猜你喜欢
Pychart connects to the remote server
buuctf-pwn write-ups (7)
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
Fabric. JS upper dash, middle dash (strikethrough), underline
What is erdma? Popular science cartoon illustration
Reuse and distribution
Development and design of animation surrounding mall sales website based on php+mysql
使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
Fabric.js 上划线、中划线(删除线)、下划线
为什么只会编程的程序员无法成为优秀的开发者?
随机推荐
Use of freemaker
检查密码
Threejs controller cube space basic controller + inertia control + flight control
tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
Simple verification code generator for 51 single chip microcomputer experiment
PHP linked list creation and traversal
微信小程序使用towxml显示公式
Reuse and distribution
Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串
Check password
Bit by bit of OpenCV calling USB camera
Openharmony notes --------- (4)
实现一个多进程并发的服务器
Xilinx Vivado set *. svh as SystemVerilog Header
Fundamentals of software testing
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
Fabric. Usage of JS eraser (including recovery function)
Advanced usage of C language -- function pointer: callback function; Conversion table
Yyds dry goods inventory software encryption lock function