当前位置:网站首页>【LeetCode 54 】螺旋矩阵
【LeetCode 54 】螺旋矩阵
2022-04-21 06:20:00 【别偷我能量】
题目描述
给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。
示例 1 :
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

示例 2 :
输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

思路

分别使用up、down、left、right来表示矩阵的边界
遍历的顺序
1.左到右 matrix[up][left]....matrix[up][right]
2.上到下 matrix[up+1][right]....matrix[down][right] //注意up+1是为了边界的缩进
3.右到左 matrix[down][right-1]....matrix[down][left] //注意right-1是为了边界的缩进
4.下到上 matrix[down+1][left]....matrix[up][left] //down-1同上
这道题主要的难点是如何去确定四条边界,初始化的时候,up就是0,down是x_len-1,left是0,right是y_len-1。按照上述遍历顺序进入while循环,每一次遍历后需要进行边界缩进和边界判断,若left>right或者up>down时,说明遍历结束,直接break结束。下文代码的核心在于每一次操作完一边(左到右、上到下、右到左和下到上)后,都会对边界进行缩进和判断。
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ans;
int x_len=matrix.size();//行数
int y_len=matrix[0].size();//列数
//将四个点的界限都标记出来
int left=0,right=y_len-1,up=0,down=x_len-1;
while(true){
//从左到右 赋值
for(int i=left;i<=right;i++)
ans.push_back(matrix[up][i]);
if(++up > down)//进行边界缩进 up=up+1操作 注意此处的++up 在up==down 之前都是进行up=up+1的更替
break;
//从上到下 此时的up的值相比较上一个for中的up已经加了1
for(int i=up;i<=down;i++)
ans.push_back(matrix[i][right]);
if(--right < left)//进行边界缩进 right=right-1操作
break;
//从右到左 注意right的值变化
for(int i=right;i>=left;i--)
ans.push_back(matrix[down][i]);
if(--down < up)//进行边界缩进 down=down-1操作
break;
//从下到上 注意down的值变化
for(int i=down;i>=up;i--)
ans.push_back(matrix[i][left]);
if(++left > right)//进行边界缩进 left=left+1操作
break;
}
return ans;
}
版权声明
本文为[别偷我能量]所创,转载请带上原文链接,感谢
https://blog.csdn.net/mitongxue/article/details/124223624
边栏推荐
- Reproduce the 3D density function diagram in the top issue of SCI
- Tensorflow case 4: MNIST handwritten numeral recognition (linear neural network) and its limitations
- Semantic feature extraction and simple word frequency display (wordcloud)
- 手势识别调研
- Analysis of distributed lock principle code using redis
- Simultaneous access of computer intranet and extranet - solution
- 【SSM整合】1. 基本环境搭建
- 反射执行FlinkSql代码时找不到UDF的class报ClassNotFound
- 【STM32&LWIP】记录一次诡异的ping不通的解决方法
- 程序员也可以写小说
猜你喜欢

Sql 按照指定内容排序

2020杭电多校赛第四场1007 Go Running(hdu6808)
![[ksz8863] information summary and board verification results of ksz8863 switch chip](/img/a0/2bfb72104d2ad3f42f1bd3121c58b4.png)
[ksz8863] information summary and board verification results of ksz8863 switch chip

【W5500】STM32 H743驱动W5500进行UDP收发

Notes on the use of STM32 h743 ECC memory

手势识别调研

为短路运算符布尔表达式添加括号
![[threadx] threadx source Reading plan (II)](/img/a8/591d22c403563ab47f99d2c009c3e2.png)
[threadx] threadx source Reading plan (II)

vscode 自定义注释
![[AD] modular schematic drawing pit point record](/img/b3/02136b8f9193d87a4780dd131f5d35.png)
[AD] modular schematic drawing pit point record
随机推荐
[SSM integration] 1 Basic environment construction
Vee validate validation
3. 事务和视图
Sql 按照指定内容排序
How to quickly "liver" high-quality slides? Recommend a latex typesetting artifact
LEFT JOIN关联表中ON,WHERE后面跟条件的区别
Learn SCI paper drawing skills (c)
R | create LEGO mosaic
【STM32】H743的25MHZ外部晶振下480MHz时钟的CubeMX配置图
Error when Linux starts MySQL
Notes on the use of STM32 h743 ECC memory
[STM32 & LwIP] record the solution of a strange Ping failure
yolov5的onnx模型去除transpose层
云计算中网络基础知识
applicationContext.xml变成灰色的文档咋解决
Swagger2生成Api文档
微服务——服务拆分策略与原则
3. Date command problem in bat
systemd如何使用/etc/init.d脚本
Analysis of distributed lock principle code using redis