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

For example,
Given n = 3,

You should return the following matrix:

[[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ]
]
Hide Tags

Array

思路:在Spiral Matrix的基础上进行修改,还是dfs

class Solution {enum direct{RIGHT = 0, DOWN, LEFT, UP};vector<int> m_res;vector<vector<int> > m_matrix;vector<vector<bool> > m_valid;int m_cnt;public:void dfs(int i, int j, enum direct d)  {   int row = m_matrix.size();int col = m_matrix[0].size();//cout << i <<",\t" << j << endl;m_cnt++;m_matrix[i][j] = m_cnt;m_valid[i][j] = false;if(d == RIGHT){if( j <= col-2 && m_valid[i][j+1])dfs(i, j+1, RIGHT);else if(i <= row-2 && m_valid[i+1][j])dfs(i+1, j, DOWN);}else if(d == DOWN){if(i <= row-2 && m_valid[i+1][j])dfs(i+1, j, DOWN);else if(j >= 1 && m_valid[i][j-1])dfs(i, j-1, LEFT);}else if(d == LEFT){if(j >= 1 && m_valid[i][j-1] )dfs(i, j-1, LEFT);else if(i >= 1 && m_valid[i-1][j])dfs(i-1, j, UP);}else if(d == UP){if(i >= 1 && m_valid[i-1][j])dfs(i-1, j, UP);else if(j <= col-1 && m_valid[i][j+1])dfs(i, j+1, RIGHT );}}
public:vector<vector<int> > generateMatrix(int n){if(n == 0)return m_matrix;// construct the matrixvector<int> row(n, 0);m_matrix.resize(n, row);//for(int i = 0; i < n; i++)//    m_matrix.push_back(row);
vector<bool> valid(n, true);m_valid.resize(n, valid);//for(int i = 0; i < n; i++)//    m_valid.push_back(valid);
m_cnt = 0;dfs(0, 0, RIGHT);return m_matrix;}
};

转载于:https://www.cnblogs.com/diegodu/p/4318326.html

[LeetCode] Spiral Matrix II相关推荐

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

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

  2. LeetCode:Spiral Matrix I II

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

  3. LeetCode 59. Spiral Matrix II

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

  4. 59. Spiral Matrix II

    /** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...

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

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

  6. C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...

  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. Leetcode: Spiral Matrix

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

  9. LeetCode59 Spiral Matrix II

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

  10. [算法][LeetCode]Spiral Matrix

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

最新文章

  1. PHP气缸种类,气缸分类方法有哪些及气缸的种类
  2. python join字符连接函数的使用方法
  3. Iptables Layer7禁止QQ、MSN、p2p软件(解决iptable 无法启动)
  4. 引用计数器法 可达性分析算法_面试官:你说你熟悉jvm?那你讲一下并发的可达性分析...
  5. Android开发笔记(五十二)通知推送Notification
  6. Windows服务一直“正在启动”怎么杀
  7. asp.net mvc redis同步mysql_Mysql和Redis数据同步策略 - 元思 - 博客园
  8. sourceinsight4.0安装破解( 内含sublime text配色方案)
  9. 从零开始的小白pr学习之旅--day2 数字后端流程(一)
  10. Excel2007打开文件时,不显示内容,需要拖拽进去才能显示
  11. COMSOL建立简单多面体
  12. 中国仪表板市场趋势报告、技术动态创新及市场预测
  13. Swin-Unet跑自己的数据集(Transformer用于语义分割)
  14. 基于单片机的测温风扇控制系统设计(#0420)
  15. 标准 BT656 并行 数据结构 详解
  16. vue-cli3 在qq浏览器不兼容
  17. 如何开发一个个人微信小程序,微信小程序开发入门教程
  18. 使用Genexus开发开源平台的应用程序
  19. Modbus学习总结
  20. Scriptable Render Pipeline-Baked Shadows

热门文章

  1. Kicad如何导入封装库、符号库(元件库)以及3D模型文件?
  2. Linux终端显示图像
  3. 浏览器异步加载和同源策略
  4. C++程序设计基础(5)sizeof的使用
  5. W-3 用grub4dos安装Windows7、Ubuntu 12.10双系统(图解)
  6. 第三个Sprint冲刺第十天
  7. 企业级监控软件使用zabbix key监控nginx status各种状态
  8. CentOS 6.5 安装 java 环境
  9. Android 字母导航条实现
  10. 官方实力榜:绿军居首黄蜂第二 火箭小降雄鹿飙升