当前位置:网站首页>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 .
边栏推荐
- LeetCode_字符串_简单_412.Fizz Buzz
- Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
- STM32 library function for GPIO initialization
- Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
- 【apipost】使用教程
- 微信小程序使用towxml显示公式
- Delete element (with transition animation)
- tmall. product. schema. Get (product information acquisition schema acquisition), Taobao store upload commodity API interface, Taobao commodity publishing interface, Taobao commodity upload API interf
- 【NOI模拟赛】伊莉斯elis(贪心,模拟)
- Database connection pool and data source
猜你喜欢

socket(套接字)与socket地址

天猫商品详情接口(APP,H5端)

Makefile 分隔文件名与后缀

富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

Yolov6 training: various problems encountered in training your dataset
[email protected] : The platform “win32“ is incompatible with this module."/>info [email protected] : The platform “win32“ is incompatible with this module.

It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen

Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting

Introduction to mathjax (web display of mathematical formulas, vector)
随机推荐
Fabric. JS manual bold text iText
LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串
C code audit practice + pre knowledge
【C语音】详解指针进阶和注意点(2)
qml 弹窗框架,可定制
实现一个多进程并发的服务器
PTA题库 ===>复数四则运算,一帮一,考试座位号(7-73)
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
复用和分用
Fabric. JS free drawing ellipse
Xilinx Vivado set *. svh as SystemVerilog Header
Have you learned the wrong usage of foreach
Fabric. Keep the original level when JS element is selected
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
C language exercises - (array)
C#代码审计实战+前置知识
CTO如何帮助业务?
mathML转latex
OpenCV调用USB摄像头的点滴
MFC CString to char*



