当前位置:网站首页>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 nMatrix , 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 .
边栏推荐
- 1、编辑利器vim
- Fabric.js 橡皮擦的用法(包含恢复功能)
- threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
- 华为面试题: 没有回文串
- [untitled] leetcode 2321 Maximum score of concatenated array
- NLA natural language analysis realizes zero threshold of data analysis
- Ad20 cannot select the solution of component packaging in PCB editor
- About text selection in web pages and counting the length of selected text
- C thread transfer parameters
- taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
猜你喜欢

Yolov6 training: various problems encountered in training your dataset

fatal: unsafe repository is owned by someone else 的解决方法
[email protected]: The platform “win32“ is incompatible with this module."/>info [email protected]: The platform “win32“ is incompatible with this module.

LeetCode 209. Minimum length subarray

buuctf-pwn write-ups (7)

JMeter script parameterization

实用调试技巧
![[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment](/img/e1/c8e81570ab78de1e488a611c25ebb9.png)
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment

Makefile 分隔文件名与后缀

Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
随机推荐
广州市应急管理局发布7月高温高湿化工安全提醒
taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
LeetCode 209. Minimum length subarray
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
微信小程序使用towxml显示公式
数据库内容输出有问题怎么解决
检查密码
Obsidian installs third-party plug-ins - unable to load plug-ins
C# 线程传参
1、编辑利器vim
buuctf-pwn write-ups (7)
【NOI模拟赛】刮痧(动态规划)
Fabric. Usage of JS eraser (including recovery function)
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
LeetCode 2310. 个位数字为 K 的整数之和
Fatal: unsafe repository is owned by someone else
Find the maximum inscribed circle of the contour
threejs的控制器 立方體空間 基本控制器+慣性控制+飛行控制
MathML to latex
CTO如何帮助业务?



