[LeetCode] Spiral Matrix II
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 ] ]
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相关推荐
- LeetCode Spiral Matrix II (生成螺旋矩阵)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- 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 ...
- LeetCode 59. Spiral Matrix II
59. Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- 59. Spiral Matrix II
/** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...
- C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...
- LeetCode 58 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Leetcode: Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode59 Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- [算法][LeetCode]Spiral Matrix
题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...
最新文章
- PHP气缸种类,气缸分类方法有哪些及气缸的种类
- python join字符连接函数的使用方法
- Iptables Layer7禁止QQ、MSN、p2p软件(解决iptable 无法启动)
- 引用计数器法 可达性分析算法_面试官:你说你熟悉jvm?那你讲一下并发的可达性分析...
- Android开发笔记(五十二)通知推送Notification
- Windows服务一直“正在启动”怎么杀
- asp.net mvc redis同步mysql_Mysql和Redis数据同步策略 - 元思 - 博客园
- sourceinsight4.0安装破解( 内含sublime text配色方案)
- 从零开始的小白pr学习之旅--day2 数字后端流程(一)
- Excel2007打开文件时,不显示内容,需要拖拽进去才能显示
- COMSOL建立简单多面体
- 中国仪表板市场趋势报告、技术动态创新及市场预测
- Swin-Unet跑自己的数据集(Transformer用于语义分割)
- 基于单片机的测温风扇控制系统设计(#0420)
- 标准 BT656 并行 数据结构 详解
- vue-cli3 在qq浏览器不兼容
- 如何开发一个个人微信小程序,微信小程序开发入门教程
- 使用Genexus开发开源平台的应用程序
- Modbus学习总结
- Scriptable Render Pipeline-Baked Shadows