当前位置:网站首页>Leetcode - Search 2D matrix
Leetcode - Search 2D matrix
2022-07-02 14:58:00 【Little astronaut w】
In daily question brushing
Catalog
Two dimensional arrays are opened in the heap
GitHub link
diwei00 (github.com)https://github.com/diwei00 All the codes are in Exercise Submit in Library
LeetCode link 🤨
subject
Write an efficient algorithm to judge
m x n
Matrix , Is there a target value . The matrix has the following characteristics :
- The integers in each row are arranged in ascending order from left to right .
- The first integer in each row is greater than the last integer in the previous row .
Example 1:
Input :matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output :trueExample 2:
Input :matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 Output :false
Topic analysis
Because the integers in each row are arranged in ascending order from left to right, and the first integer in each row is greater than the last integer in the previous row , In fact, it means that the data of the whole matrix is arranged in ascending order , It's just a matrix . According to this characteristic, we first need to traverse the data of the last column in the matrix , If this column of data is larger than the target value target Big , That is, the data to be searched must be in this line , Then you only need to traverse this row of data .
In fact, it is found that the data in the last column is also in ascending order , And each row is also arranged in ascending order , That means we can use the binary search method when traversing the last column and a row of data , But the last number to look up appears on a certain line , Therefore, the binary search method is used when traversing rows .
Function parameter analysis :
Generally, the two-dimensional array parameter transmission developed is to directly transmit the address of the first row of the address array , Use array pointers to receive . You can see that the receiving two-dimensional array here is a secondary pointer , In fact, this two-dimensional array is used in the heap malloc Opened up . First open up an array of storage addresses , Then open up many arrays , Save the first address of these arrays , That is, it forms a two-dimensional array . On release , Also release parr Array of this piece , And then release pparr.( There is code implementation behind )matrixSize Is the number of rows of the matrix ,matrixColSize Is the address of the number of matrix columns ,target Is the number of targets to find .
Code implementation ( Sequential search method )
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 Cycle out , Fixed subscript
if (i == row)
{
i -= 1;
}
int j = 0;
for (j = 0; j < col; j++)
{
if (matrix[i][j] == target)
{
return true;
}
}
return false;
}
Code implementation ( Dichotomy search )
bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target)
{
int row = matrixSize;
int col = *matrixColSize;
int i = 0;
// Find the line that needs to find the target value
for (i = 0; i < row; i++)
{
if (matrix[i][col - 1] > target || matrix[i][col - 1] == target)
{
break;
}
}
//for Cycle out , Fixed subscript
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 If the loop jumps out, the target value cannot be found
return false;
}
Code implementation ( Two dimensional arrays are opened in the heap )
int main()
{
// Save an array of addresses
int** pa = (int**)malloc(sizeof(int) * 3);
int i = 0;
int col = 3;
// Save the array address pa in
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;
}
Summary
There are many algorithms for each problem , We should open our minds , Continue to worry about your own algorithm , I will also have a different harvest .
边栏推荐
- Fabric. JS free draw circle
- obsidian安装第三方插件——无法加载插件
- Fabric. Usage of JS eraser (including recovery function)
- STM32 standard firmware library function name memory (II)
- Fabric. JS free drawing ellipse
- 一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
- qml 弹窗框架,可定制
- Makefile 分隔文件名与后缀
- taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
- Simple verification code generator for 51 single chip microcomputer experiment
猜你喜欢
Simple verification code generator for 51 single chip microcomputer experiment
fatal: unsafe repository is owned by someone else 的解决方法
LeetCode 2320. 统计放置房子的方式数
Tmall product details interface (APP, H5 end)
Fabric. JS zoom canvas
Advanced C language (realize simple address book)
Wechat applet uses towxml to display formula
STM32库函数进行GPIO初始化
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
LeetCode 2310. 个位数字为 K 的整数之和
随机推荐
mathjax 入门(web显示数学公式,矢量的)
LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串
Introduction to C language -- array
【C语音】详解指针进阶和注意点(2)
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
PTA question bank== > complex four operations, one for one, examination seat number (7-73)
Advanced C language (realize simple address book)
4. Array pointer and pointer array
Arithmetic operations and related exercises in C language
Delete element (with transition animation)
复用和分用
传感器数据怎么写入电脑数据库
Fabric. JS dynamically set font size
为什么只会编程的程序员无法成为优秀的开发者?
C code audit practice + pre knowledge
4、数组指针和指针数组
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
Xilinx Vivado set *.svh as SystemVerilog Header
3. Function pointers and pointer functions
JMeter script parameterization