当前位置:网站首页>Search of two-dimensional array of "sword finger offer" C language version
Search of two-dimensional array of "sword finger offer" C language version
2022-07-24 06:01:00 【Zonggu22】
List of articles
- Preface
- title
- Picture demonstration target = 7
- 1、 Traverse from the element in the lower left corner of the matrix , Compare it with the target value
- 2、 Elements 18 Greater than the target value 7, That's ok `row` The index moves up one space
- 3、 Elements 10 Greater than the target value 7, That's ok `row` The index moves up one space
- 4、 Elements 3 Less than the target 7, Column `column` The index moves one space to the right
- 5、 Elements 8 Greater than the target value 7, That's ok `row` The index moves up one space
- 6、 Elements 5 Less than the target 7, Lasso moves one space to the right
- 7、 Find the target value , return `true`
- Code implementation
Preface
Reference article : There is a better animated version
The title is 《 The finger of the sword Offer》 Algorithm problem of the second edition .
The title is :
In a n * m In a two-dimensional array , Each row is sorted in ascending order from left to right , Each column is sorted in ascending order from top to bottom . Please complete a function , Enter such a two-dimensional array and an integer , Determine whether the array contains the integer .
Example :
The existing matrix matrix as follows :
[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30] ]Given target = 5, return true.
Given target = 20, return false.
Limit :
- 0 <= n <= 1000
- 0 <= m <= 1000
title
Observation matrix , You can find : The lower left element Is the largest element in the column , The smallest element in the row
If The lower left element Greater than the target value , Then the target value must be above the line , The line of the element in the lower left corner can be deleted
If The lower left element Less than the target value , The target value must be to the right of the column , The column of the element in the lower left corner can be eliminated
The specific operation is to traverse from the lower left corner of the matrix , And compare with the target value :
- row It's the number of lines column It's the number of columns
- When matrix [row] [column] > target when : The line leads up one space ( namely row– ), That is to say, eliminate the second part of the matrix row Row element
- When matrix [row] [column] < target when : The line leads up one space ( namely column++ ), That is to say, eliminate the second part of the matrix column Column elements
- When matrix [row] [column] < target when : return true.
- If you cross the border , return false.
Picture demonstration target = 7
1、 Traverse from the element in the lower left corner of the matrix , Compare it with the target value
| 1 | 4 | 6 | 9 | 15 | |
|---|---|---|---|---|---|
| 2 | 5 | 7 | 12 | 19 | |
| 3 | 8 | 9 | 16 | 22 | |
| 10 | 13 | 15 | 18 | 23 | |
| row | 18 | 19 | 23 | 25 | 28 |
| column |
2、 Elements 18 Greater than the target value 7, That's ok row The index moves up one space
| 1 | 4 | 6 | 9 | 15 | |
|---|---|---|---|---|---|
| 2 | 5 | 7 | 12 | 19 | |
| 3 | 8 | 9 | 16 | 22 | |
| row | 10 | 13 | 15 | 18 | 23 |
| 18 | 19 | 23 | 25 | 28 | |
| column |
At this time, the original line does not meet the requirements , Delete the original line
| 1 | 4 | 6 | 9 | 15 | |
|---|---|---|---|---|---|
| 2 | 5 | 7 | 12 | 19 | |
| 3 | 8 | 9 | 16 | 22 | |
| row | 10 | 13 | 15 | 18 | 23 |
| column |
3、 Elements 10 Greater than the target value 7, That's ok row The index moves up one space
| 1 | 4 | 6 | 9 | 15 | |
|---|---|---|---|---|---|
| 2 | 5 | 7 | 12 | 19 | |
| row | 3 | 8 | 9 | 16 | 22 |
| column |
At this time, the original line does not meet the requirements , Delete the original line
4、 Elements 3 Less than the target 7, Column column The index moves one space to the right
| 4 | 6 | 9 | 15 | ||
|---|---|---|---|---|---|
| 5 | 7 | 12 | 19 | ||
| row | 8 | 9 | 16 | 22 | |
| column |
At this time, the original column does not meet the requirements , Delete the original column
5、 Elements 8 Greater than the target value 7, That's ok row The index moves up one space
| 4 | 6 | 9 | 15 | ||
|---|---|---|---|---|---|
| row | 5 | 7 | 12 | 19 | |
| column |
At this time, the original line does not meet the requirements , Delete the original line
6、 Elements 5 Less than the target 7, Lasso moves one space to the right
| 6 | 9 | 15 | |||
|---|---|---|---|---|---|
| row | 7 | 12 | 19 | ||
| column |
At this time, the original column does not meet the requirements , Delete the original column
7、 Find the target value , return true
Code implementation
#include <stdio.h>
#define true 1
#define false 0
int Find(int array[][5], int rows, int columns, int num) //row Is the number of rows of the original array ,columns Is the number of columns in the original array
{
int row; // Number of lines in the loop ( Note that there is no s)
int column; // Number of circular columns ( Note that there is no s)
row = 4; // Start with the last line
column = 0; // Start with the first in the last line ( The lower left corner )
if (rows == 0 || columns == 0)
{
return false;
}
while (column < columns && row >= 0)
{
if (array[row][column] == num)
{
return true;
}
else if (array[row][column] < num)
{
column++; // To the right
}
else
{
row--; // Move up
}
}
return false;
}
void main()
{
int row; // Row number
int column; // Number of columns
int num; // Find the target target
int a[5][5] = {
{
1,4,7,11,15},
{
2,5,8,12,19},
{
3,6,9,16,22},
{
10,13,14,17,24},
{
18,21,23,26,30}
};
row = sizeof(a) / sizeof(a[0]); // Find the number of rows
column = sizeof(a[0]) / sizeof(int); // Find the number of columns
printf(" Please enter the number you want to enter :");
scanf("%d", &num);
printf("%d ( if 1 I found it , if 0 We didn't find out )", Find(a,row,column,num));
}
边栏推荐
- day1-jvm+leetcode
- PDF Text merge
- Signals and systems: Hilbert transform
- Commands for quickly opening management tools
- Machine learning (zhouzhihua) Chapter 2 model selection and evaluation notes learning experience
- 列表写入txt直接去除中间的逗号
- jupyter notebook一直自动重启(The kernel appears to have died. It will restart automatically.)
- [MYCAT] MYCAT sets up read-write separation
- [deep learning] teach you to write "handwritten digit recognition neural network" hand in hand, without using any framework, pure numpy
- 【USB Host】STM32H7 CubeMX移植带FreeRTOS的USB Host读取U盘,USBH_Process_OS卡死问题,有个值为0xA5A5A5A5
猜你喜欢

Loss after cosine annealing decay of learning rate
![[MYCAT] MYCAT configuration file](/img/53/63a633d3ae917e3754f9f7f5c6567f.png)
[MYCAT] MYCAT configuration file

主成分分析计算步骤

谷歌/火狐浏览器管理后台新增账号时用户名密码自动填入的问题

STM32 DSP library MDK vc5\vc6 compilation error: 256, (const float64_t *) twiddlecoeff64_ 256, armBitRevIndexTableF64_ 256,
![[activiti] personal task](/img/bc/80ac4067f6c58785acb4273f9507ee.png)
[activiti] personal task

如何在网页上下载视频

AD1256

A small problem in labelme to VOC code
![[activiti] process example](/img/77/63d7d1ea8c634fb3741383f40f38e7.png)
[activiti] process example
随机推荐
Accurate calculation of time delay detailed explanation of VxWorks timestamp
[MYCAT] MYCAT sets up read-write separation
A small problem in labelme to VOC code
KMP代码分布详解
Qt新建工程简介
目标检测带标签数据增强代码
单播、组播、广播、工具开发、QT Udp通讯协议开发简介及开发工具源码
Statistical learning methods (2nd Edition) Li Hang Chapter 22 summary of unsupervised learning methods mind mapping notes
"Statistical learning methods (2nd Edition)" Li Hang Chapter 15 singular value decomposition SVD mind map notes and after-school exercise answers (detailed steps) SVD matrix singular value Chapter 15
主成分分析计算步骤
[deep learning] teach you to write "handwritten digit recognition neural network" hand in hand, without using any framework, pure numpy
对ArrayList<ArrayList<Double>>排序
PDF Text merge
day-7 jvm完结
"Statistical learning methods (2nd Edition)" Li Hang Chapter 17 latent semantic analysis LSA LSI mind mapping notes and after-school exercise answers (detailed steps) Chapter 17
systemctl + journalctl
Could not load library cudnn_ cnn_ infer64_ 8.dll. Error code 126Please make sure cudnn_ cnn_ infer64_ eight
[activiti] activiti system table description
如何解决训练集和测试集的分布差距过大问题
解决ModularNotFoundError: No module named “cv2.aruco“