当前位置:网站首页>Sword finger offer 29 Print matrix clockwise
Sword finger offer 29 Print matrix clockwise
2022-06-11 21:44:00 【Code loving students】
Title Description :
Enter a matrix , Print out each number in a clockwise order from the outside in .
Topic analysis :
Print in clockwise order , Then we can divide it into four steps :
1. From left to right
2. From the top down
3. From right to left
4. From bottom to top
Therefore, we can express the row or column of the four edges of the matrix respectively .
After each printing, we print the row or column in which it is located +-1, Used to reduce the matrix , To show that we have gone through this section .
The code is as follows :
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize){
if(matrixSize==0){
*returnSize=0;
return NULL;
}
*returnSize=matrixSize*(*matrixColSize);// Find the number of elements in the matrix
int up=0,down=matrixSize-1;// Four borders
int left=0,right=*matrixColSize-1;
int*ret=(int*)malloc(sizeof(int)*(*returnSize));// Construct an array
int index=0;
while(index<*returnSize){
for(int i=left;index<*returnSize&&i<=right;i++){// From the left -> Right
ret[index++]=matrix[up][i];
}
up++; // Reduce the matrix
for(int i=up;index<*returnSize&&i<=down;i++){// From above -> Next
ret[index++]=matrix[i][right];
}
right--;
for(int i=right;index<*returnSize&&i>=left;i--){// From the right -> Left
ret[index++]=matrix[down][i];
}
down--;
for(int i=down;index<*returnSize&&i>=up;i--){// From below -> On
ret[index++]=matrix[i][left];
}
left++;
}
return ret;
}边栏推荐
- Deploy SAP ui5 applications to the sap BTP kyma operating environment step by step
- LeetCode-98-验证二叉搜索树
- 联调这夜,我把同事打了...
- LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)
- bzoj3188 Upit
- JVM | local method interface; Native Method Stack
- 實驗10 Bezier曲線生成-實驗提高-控制點生成B樣條曲線
- 杭电中超9 1006 Guess the Weight
- Redis basic data type (hash)
- LeetCode-76-最小覆盖子串
猜你喜欢

LeetCode-110-平衡二叉树

LabVIEW Arduino electronic weighing system (project Part-1)
![[Part 14] source code analysis and application details of completionservice class [key]](/img/41/9f5383d6eafb32723e29c15da3a1af.jpg)
[Part 14] source code analysis and application details of completionservice class [key]

189. 轮转数组

Jenkins+allure integrated report construction

The upcoming launch of the industry's first retail digital innovation white paper unlocks the secret of full link digital success

apache 本地多端口配置
![[Part 15] use and basic principle of forkjoinpool [key]](/img/36/e21b16ec92d444149bc793f340f9f3.jpg)
[Part 15] use and basic principle of forkjoinpool [key]

Leetcode-98- validate binary search tree

Leetcode-32- longest valid bracket
随机推荐
Answer fans' questions | count the number and frequency of letters in the text
How to manually send events exposed by SAP commerce cloud mock application using SAP kyma console
Rexroth overflow valve zdb6vp2-42/315v
快速排序的非递归写法
Look for leap years and see how many leap years I have had since I was born (I have had five)
Leetcode-155-minimum stack
Flink error: multiple tasks are started, and only one task is executed
AC自动机
2021-09-11 训练场补题
杭电中超9 1006 Guess the Weight
flutter系列之:flutter中常用的container layout详解
[Part 15] use and basic principle of forkjoinpool [key]
Leetcode-110-balanced binary tree
如何利用RPA机器人开启货代行业数字化转型第一步?
2021牛客多校5 Double Strings
Hangzhou Electric Zhongchao 91006 guess the weight
Codeworks round 739 (Div. 3) problem solving Report
Endnotex9 introduction and basic tutorial instructions
继承的所有特征
LeetCode-32-最长有效括号