[leetcode-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].
思路:
上下左右4个变量分别表示边界,如果越界,结束循环。
vector<int> spiralOrder(vector<vector<int>>& matrix){if (matrix.empty())return{};int m = matrix.size(), n = matrix[0].size();vector<int> spiral(m*n);int up = 0, down = m - 1, left = 0, right = n - 1, k = 0;while (1){for (int col = left; col <= right; col++)spiral[k++] = matrix[up][col];if (++up > down)break;for (int row = up; row <= down; row++)spiral[k++] = matrix[row][right];if (--right < left)break;for (int col = right; col >= left; col--)spiral[k++] = matrix[down][col];if (--down < up)break;for (int row = down; row >= up; row--)spiral[k++] = matrix[row][left];if (++left > right)break; }return spiral;}
参考:
https://discuss.leetcode.com/topic/21090/0ms-clear-c-solution
转载于:https://www.cnblogs.com/hellowooorld/p/6930538.html
[leetcode-54-Spiral Matrix]相关推荐
- LeetCode 54. Spiral Matrix
54. Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the m ...
- 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 ...
最新文章
- windows系统下Python环境的搭建
- React事件优雅绑定
- 每日一笑 | 程序员的招租公告
- #6278. 数列分块 2 分块 + 块内二分
- 包无法安装_详细教程 | 安装Python编程环境以及使用OpenpyXl操作Excel
- 一文读懂区块链以及一个区块链的实现
- 上海师范大学计算机技术考研分数线,上海师范大学2019年考研复试分数线已公布...
- mysql update 批量更新_mysql 批量更新的两种方法
- 利用SQL语句自动生成序号的两种方式
- 2016计算机二级java_2016年计算机二级《JAVA》考试练习题
- ckpt下载 deeplabv3_Ubantu下 用deeplabV3+训练自己的数据集 你可能遇到的所有坑都在这了...
- 惠新宸:我也曾经是“不适合”编程的人(图灵访谈)
- 倾斜摄影模型(.osgb)中心点位置的确定方法(SuperMap idesktop)
- 51单片机-波特率的计算定时器初始值的计算
- Git提交项目到GitHub完整流程
- python中用pygame创建一个精致的窗口
- 数控加工仿真系统 - FANUC OI 铣床编程笔记(上)
- ADPCM 编码 及WAV解析 及实例
- 从SEO优化角度打造移动端网站的移动建站指南
- 页面预加载loading动画,再载入内容