LeetCode 54. Spiral Matrix
54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
题目大意:给一个m*n的矩阵,以顺时针旋转的方式返回所有的元素到一个数组中~
分析:按照一个个矩阵的边框输出:x、m为行的上下界,y、n为列的上下界,每次输出这个围成的矩阵的第一行——最后一列——最后一行——第一列,然后将x和y自增1,m和n自减1~
注意:为了避免重复输出,当x和m相等或者y和n相等的时候,就输出一次第一行和最后一列就可以,不用重复输出最后一行和第一列~
class Solution { public:vector<int> spiralOrder(vector<vector<int>>& matrix) {vector<int> result;if(matrix.empty()) return result;int m = matrix.size() - 1, n = matrix[0].size() - 1;for (int x = 0, y = 0; x <= m && y <= n; x++, y++, m--, n--) {for (int j = y; j <= n; j++)result.push_back(matrix[x][j]);for (int i = x + 1; i <= m - 1; i++)result.push_back(matrix[i][n]);for (int j = n; j >= y && x != m; j--)result.push_back(matrix[m][j]);for (int i = m - 1; i > x && y != n; i--)result.push_back(matrix[i][y]);}return result;} };
LeetCode 54. Spiral Matrix相关推荐
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- leetcode 54. Spiral Matrix | 54. 螺旋矩阵(Java)
题目 https://leetcode.com/problems/spiral-matrix/ 题解 类似于状态机的思路,设定一个 padding,走一圈之后,padding+1 .用 长宽 - pa ...
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...
- LeetCode 54. Spiral Matrix--Python解法--螺旋排序
题目地址:Spiral Matrix - LeetCode Given a matrix of m x n elements (m rows, n columns), return all eleme ...
- LeetCode 59. Spiral Matrix II
59. Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- LeetCode 58 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 54. Spiral Matrix
description: 螺旋输出一个矩阵. Note: Example: Example 1:Input: [[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ] ] Output ...
- 54. Spiral Matrix (Matrix)
设置toprow bottomrow leftcol rightcol来标记边界,然后对每一条边界进行循环 要是list的size等于matrix的size的话 就表明结束了 1 class Solu ...
- LeetCode Spiral Matrix II (生成螺旋矩阵)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
最新文章
- Git版本库创建(包含文件权限设置 Linux环境下)
- Java NIO框架Netty教程(一) – Hello Netty
- 体二极管的原理及应用
- 试题 历届试题 买不到的数目(dp/数学)
- vscode中go插件配置
- php函数大全 pdf,php常用函数.pdf
- c#12星座速配代码_原来12星座的软件工程师是这样的
- Matlab小波工具箱的使用2
- tm8211的i2s协议
- 深入浅出、通俗易懂的讲解CAN bus。
- mysql 统计每天、每周、每月、每年数据
- 横向浅析Small,RePlugin两个插件化框架
- [足式机器人]Part3机构运动微分几何学分析与综合Ch01-1 平面运动微分几何学——【读书笔记】
- B站增量数据湖探索与实践
- 我要减肥~~~~~!!!(信誓旦旦)
- 一键 为 Win7 安装盘 注入 USB 3.0驱动
- Android 解决帧动画卡顿问题
- 运算器设计(计算机组成实验)1
- Linux基础笔记11 | 链接文件详解
- Python制图常用设置