当前位置:网站首页>剑指Offer 29.顺时针打印矩阵
剑指Offer 29.顺时针打印矩阵
2022-06-11 21:36:00 【爱学代码的学生】
题目描述:
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
题目分析:
以顺时针的顺序打印,那我们可以将其分成四个步骤:
1. 从左往右
2. 从上往下
3. 从右往左
4. 从下往上
因此我们可以将矩阵的四个边所在的行或列分别表示出来。
每一次打印完以后我们将其所在的行或者列进行+-1,用来缩小矩阵,来说明我们已经走过此段。
代码如下:
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize){
if(matrixSize==0){
*returnSize=0;
return NULL;
}
*returnSize=matrixSize*(*matrixColSize);//求出矩阵中元素个数
int up=0,down=matrixSize-1;//四个边界
int left=0,right=*matrixColSize-1;
int*ret=(int*)malloc(sizeof(int)*(*returnSize));//构造数组
int index=0;
while(index<*returnSize){
for(int i=left;index<*returnSize&&i<=right;i++){//从左->右
ret[index++]=matrix[up][i];
}
up++; //缩小矩阵
for(int i=up;index<*returnSize&&i<=down;i++){//从上->下
ret[index++]=matrix[i][right];
}
right--;
for(int i=right;index<*returnSize&&i>=left;i--){//从右->左
ret[index++]=matrix[down][i];
}
down--;
for(int i=down;index<*returnSize&&i>=up;i--){//从下->上
ret[index++]=matrix[i][left];
}
left++;
}
return ret;
}边栏推荐
- Answer fans' questions | count the number and frequency of letters in the text
- Codeforces Round #740 Div. 2 解题报告
- Servlet get form data
- [Part 16] copyonwritearraylist source code analysis and application details [key]
- Endnotex9 introduction and basic tutorial instructions
- Educational Codeforces Round 114 (Rated for Div. 2) D
- 网络连接正常但百度网页打不开显示无法访问此网站解决方案
- 實驗10 Bezier曲線生成-實驗提高-控制點生成B樣條曲線
- As a senior abap consultant, which SAP technology can be selected as the main direction in the next step?
- Deploy SAP ui5 applications to the sap BTP kyma operating environment step by step
猜你喜欢

LabVIEW Arduino electronic weighing system (project Part-1)

LeetCode-155-最小栈

Test plans and test cases

LeetCode-32-最长有效括号

关于斜率优化

LeetCode-322-零钱兑换

RPA super automation | nongnongji and cloud expansion accelerate financial intelligent operation

Jenkins+allure integrated report construction

JVM | virtual machine stack (local variable table; operand stack; dynamic link; method binding mechanism; method call; method return address)

Apache local multi port configuration
随机推荐
一个Golang的私有库设置问题
[Part 15] use and basic principle of forkjoinpool [key]
线性表的链式存储结构
使用 SAP UI5 CLI 命令行工具构建和运行 SAP UI5 应用
Codeforces Round #744 (Div. 3) 解题报告
【C語言進階】整型在內存中的存儲
BZOJ3189 : [Coci2011] Slika
Educational Codeforces Round 114 (Rated for Div. 2) D
Leetcode-104- maximum depth of binary tree
JVM | virtual machine stack (local variable table; operand stack; dynamic link; method binding mechanism; method call; method return address)
Flutter implements the JD address selection component
Codeworks round 744 (Div. 3) problem solving Report
Codeforces Round #742 (Div. 2) F. One-Four Overload
JVM | local method interface; Native Method Stack
LeetCode-155-最小栈
RPA super automation | nongnongji and cloud expansion accelerate financial intelligent operation
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
Codeforces Round #739 (Div. 3)解题报告
How to use RPA robot to start the first step of digital transformation of freight forwarding industry?
How does the chief financial officer of RPA find the "super entrance" of digital transformation?