当前位置:网站首页>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;
}边栏推荐
- Refresh and upgrade | innovation, starting from cloud store
- Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?
- 即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
- LeetCode-43-字符串相乘
- EndnoteX9简介及基本教程使用说明
- Leetcode-76- minimum covering substring
- 剑指Offer 29.顺时针打印矩阵
- 如何利用RPA机器人开启货代行业数字化转型第一步?
- JVM|运行时数据区;程序计数器(PC寄存器);
- LeetCode-98-验证二叉搜索树
猜你喜欢

Why is rpa+ low code a powerful tool to accelerate the digital transformation of finance?

Answer fans' questions | count the number and frequency of letters in the text

2021牛客多校5 Double Strings

Leetcode-322- change exchange

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

JVM|虚拟机栈(局部变量表;操作数栈;动态链接;方法的绑定机制;方法的调用;方法返回地址)

RPA超自动化 | 农耕记携手云扩加速财务智能化运营

C语言实现迷宫问题

联调这夜,我把同事打了...

The network connection is normal, but Baidu web page can not be opened and displayed. You can't access this website solution
随机推荐
Rexroth overflow valve zdb6vp2-42/315v
LaTex实战笔记 3-宏包与控制命令
焕新升级 | 创新,从云商店开始
EndnoteX9簡介及基本教程使用說明
As a senior abap consultant, which SAP technology can be selected as the main direction in the next step?
RPA超自动化 | 农耕记携手云扩加速财务智能化运营
Codeworks round 740 Div. 2 problem solving Report
RPA+低代码助推品牌电商启新创变、重启增长
Why is rpa+ low code a powerful tool to accelerate the digital transformation of finance?
每日一题 -- 验证回文串
Using the sap ui5 cli command line tool to build and run SAP ui5 applications
bzoj3188 Upit
Usage of esp32c3 Arduino Library
實驗10 Bezier曲線生成-實驗提高-控制點生成B樣條曲線
[Part 13] source code analysis and application details of completabilefuture class [key]
Redis basic data type (list)
JS performs non empty judgment on various data types of the returned data.
bzoj3188 Upit
二分查找 - 学习
Jenkins+allure integrated report construction