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. [ 1, 2, 3 ],
  3. [ 4, 5, 6 ],
  4. [ 7, 8, 9 ]
  5. ]

You should return [1,2,3,6,9,8,7,4,5].

典型的dfs问题,与前面一个问题比较像,代码如下。单独维护一个数组记录是否已经走过某个格子,注意边界条件的判断就可以了,代码如下:

  1. class Solution {
  2. public:
  3. vector<int> spiralOrder(vector<vector<int>>& matrix) {
  4. drt = vector<vector<int>>{{,},{,},{-,},{,-}}; //四个方向
  5. mark = vector<vector<bool>>(,vector<bool>(,false));
  6. ret.clear();
  7. if(!matrix.size() || !matrix[].size())
  8. return ret;
  9. dfs(matrix, , , -);//这里取-1是为了能从(0,0)开始。
  10. return ret;
  11. }
  12.  
  13. void dfs(vector<vector<int>> & matrix, int direct, int x, int y)
  14. {
  15. for(int i = ; i < ; ++i){
  16. int j = (direct + i) % ;
  17. int tx = x + drt[j][];
  18. int ty = y + drt[j][];
  19. if(tx >= && tx < matrix.size() &&
  20. ty >= && ty < matrix[].size() &&
  21. mark[tx][ty] == false){
  22. mark[tx][ty] = true;
  23. ret.push_back(matrix[tx][ty]);
  24. dfs(matrix, j, tx, ty);
  25. }
  26. }
  27. }
  28.  
  29. private:
  30. vector<vector<int>> drt;
  31. vector<vector<bool>> mark;
  32. vector<int> ret;
  33. };

LeetCode OJ: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 t ...

  2. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  3. [leetcode]54. Spiral Matrix螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  4. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  5. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  6. PAT甲级——1105 Spiral Matrix (螺旋矩阵)

    此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...

  7. [算法][LeetCode]Spiral Matrix——螺旋矩阵

    题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...

  8. 第29题:LeetCode54:Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

  9. Leetcode54. Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

  10. spiral matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

随机推荐

  1. struts 文件下载 annotation 注解版

    [本文简介] 本文将简单介绍使用 struts2 ,通过零配置和 annotation 实现文件下载功能. [文件夹结构] [web.xml有关struts的配置] <filter> &l ...

  2. Android_Kotlin 代码学习

    https://github.com/ldm520/Android_Kotlin_Demo

  3. eslasticsearch操作集锦

    索引-index:一个索引就是一个拥有几分相似特征的文档的集合.比如说,你可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引.一个索引由一个名字来标识(必须全部是小写字母的),并且 ...

  4. sqlite时间段查询

    同样的SQL语句,查不出数据来 select * from table1 where t1>='2017-6-1' and t1<='2017-6-5' 改成 select * from ...

  5. VGA显示

    VGA控制器的编写主要是了解VGA的显示标准和时序,如1024X768@60Hz,确定时钟频率(65MHz=1344X806X60),列像素时间等于时钟周期,扫描从左到右.从上到下(类似于电视扫描PA ...

  6. 中文Appium API 文档

    该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...

  7. python全栈开发从入门到放弃之字典的应用

    1.存值 info_dic={'name':'egon','age':18,'sex':'male'} info_dic['job']='IT' #根据key来存值 print(info_dic) 输 ...

  8. .net:easyui-datagrid清空表中原有数据

    $("#StudentTable").datagrid("loadData", { total: 0, rows: [] });

  9. 三道半平面交测模板题 Poj1474 Poj 3335 Poj 3130

    求半平面交的算法是zzy大神的排序增量法. ///Poj 1474 #include <cmath> #include <algorithm> #include <cst ...

  10. 杭电1025Constructing Roads In JGShining's Kingdom

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1025 题目: Problem Description JGShining's kingdom consis ...