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]相关推荐

  1. 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 ...

  2. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  3. leetcode 54. Spiral Matrix | 54. 螺旋矩阵(Java)

    题目 https://leetcode.com/problems/spiral-matrix/ 题解 类似于状态机的思路,设定一个 padding,走一圈之后,padding+1 .用 长宽 - pa ...

  4. LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...

  5. LeetCode 54. Spiral Matrix--Python解法--螺旋排序

    题目地址:Spiral Matrix - LeetCode Given a matrix of m x n elements (m rows, n columns), return all eleme ...

  6. LeetCode 59. Spiral Matrix II

    59. Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  7. LeetCode 58 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. 54. Spiral Matrix

    description: 螺旋输出一个矩阵. Note: Example: Example 1:Input: [[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ] ] Output ...

  9. 54. Spiral Matrix (Matrix)

    设置toprow bottomrow leftcol rightcol来标记边界,然后对每一条边界进行循环 要是list的size等于matrix的size的话 就表明结束了 1 class Solu ...

  10. LeetCode Spiral Matrix II (生成螺旋矩阵)

     Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...

最新文章

  1. windows系统下Python环境的搭建
  2. React事件优雅绑定
  3. 每日一笑 | 程序员的招租公告
  4. #6278. 数列分块 2 分块 + 块内二分
  5. 包无法安装_详细教程 | 安装Python编程环境以及使用OpenpyXl操作Excel
  6. 一文读懂区块链以及一个区块链的实现
  7. 上海师范大学计算机技术考研分数线,上海师范大学2019年考研复试分数线已公布...
  8. mysql update 批量更新_mysql 批量更新的两种方法
  9. 利用SQL语句自动生成序号的两种方式
  10. 2016计算机二级java_2016年计算机二级《JAVA》考试练习题
  11. ckpt下载 deeplabv3_Ubantu下 用deeplabV3+训练自己的数据集 你可能遇到的所有坑都在这了...
  12. 惠新宸:我也曾经是“不适合”编程的人(图灵访谈)
  13. 倾斜摄影模型(.osgb)中心点位置的确定方法(SuperMap idesktop)
  14. 51单片机-波特率的计算定时器初始值的计算
  15. Git提交项目到GitHub完整流程
  16. python中用pygame创建一个精致的窗口
  17. 数控加工仿真系统 - FANUC OI 铣床编程笔记(上)
  18. ADPCM 编码 及WAV解析 及实例
  19. 从SEO优化角度打造移动端网站的移动建站指南
  20. 页面预加载loading动画,再载入内容

热门文章

  1. pgsql自动安装shell脚本整理
  2. Hyper-V复制功能
  3. DevExpress VCL Controls v15.1.5正式发布[附下载]
  4. android开发Proguard混淆与反射
  5. windows下apk查看工具的原理
  6. 11/100. Convert BST to Greater Tree
  7. java 对象重写tostring
  8. 清北学堂(2019 4 28 ) part 1
  9. Linux 查看网卡流量【转】
  10. 20145214 《Java程序设计》第3周学习总结