题意:  

  从1开始产生连续的n2个数字,以螺旋的方式填满一个n*n的数组。

思路:

  由于是填满一个矩阵,那么只需要每次都填一圈即可。应该注意特殊情况。

  迭代:

 class Solution {
public:
vector<vector<int> > generateMatrix(int n)
{
vector<vector<int> > ans(n,vector<int>(n));
int cnt=, i=;
while()
{
if(cnt==n*n) break;
for(int j=i; j<n-i; j++) ans[i][j]=++cnt;
for(int j=i+; j<n-i; j++) ans[j][n-i-]=++cnt;
for(int j=n-i-; j>=i; j--)ans[n-i-][j]=++cnt;
for(int j=n-i-; j>i; j--) ans[j][i]=++cnt;
i++;
}
return ans;
}
};

AC代码

  

  递归:  

 class Solution {
public:
vector<vector<int> > ans;
int n;
void generate(int i,int cnt)
{
if(cnt==n*n) return ;
for(int j=i; j<n-i; j++) ans[i][j]=++cnt;
for(int j=i+; j<n-i; j++) ans[j][n-i-]=++cnt;
for(int j=n-i-; j>=i; j--)ans[n-i-][j]=++cnt;
for(int j=n-i-; j>i; j--) ans[j][i]=++cnt;
generate(i+,cnt);
} vector<vector<int> > generateMatrix(int n)
{
ans=vector<vector<int> >(n,vector<int>(n));
this->n=n;
generate(, );
return ans;
}
};

AC代码

LeetCode Spiral Matrix II (技巧)的更多相关文章

  1. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  2. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

  3. [Leetcode] spiral matrix ii 螺旋矩阵

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

  4. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  5. Leetcode Spiral Matrix II

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

  6. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  7. 【leetcode】Spiral Matrix II

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

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

  9. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

随机推荐

  1. C语言内存分配机制

    内存分配方式有三种: (1)从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量. (2)在栈上创建.在执行函数时,函数内局部变量的 ...

  2. lucene底层数据结构——FST,针对field使用列存储,delta encode压缩doc ids数组,LZ4压缩算法

    参考: http://www.slideshare.net/lucenerevolution/what-is-inaluceneagrandfinal http://www.slideshare.ne ...

  3. netty4 Handler的执行顺序

    转载:https://my.oschina.net/jamaly/blog/272385 Handler在netty中,无疑占据着非常重要的地位.Handler与Servlet中的filter很像,通 ...

  4. [转]C# 应用程序安装部署步骤,安装前操作,先退出程序后卸载。

    1. 点击[文件]-[新建]-[项目]-其他项目类型-安装和部署,选择安装项目,在下面的名称栏填写SetupTest(或者选择安装向导,一直点击[下一步])2. 安装项目----六个子项依次为:文件系 ...

  5. Log4J实用配置指南

    转自:http://www.cnblogs.com/licheng/archive/2008/08/23/1274566.html 1         概述 本文档是针对Log4j日志工具的使用指南. ...

  6. Ganglia监控Hadoop集群的安装部署[转]

    Ganglia监控Hadoop集群的安装部署 一. 安装环境 Ubuntu server 12.04 安装gmetad的机器:192.168.52.105 安装gmond的机 器:192.168.52 ...

  7. python使用urllib2抓取网页

    1.使用python的库urllib2,用到urlopen和Request方法. 2.方法urlopen原形 urllib2.urlopen(url[, data][, timeout]) 其中: u ...

  8. Android统计图表MPAndroidChart.

    Android统计图表MPAndroidChart MPAndroidChart是在Android平台上开源的第三方统计图表库,可以绘制样式复杂.丰富的各种统计图表,如一般常见的折线图.饼状图.柱状图 ...

  9. IT公司100题-6-根据上排给出十个数,在其下排填出对应的十个数

    问题描述: 给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数要求下排每个数都是先前上排那十个数在下排出现的次数.上排的十个数如下:[0,1,2,3,4,5,6,7,8,9] 举一个例子, ...

  10. POJ 3648 2-sat

    题目大意: 有一对新人结婚,邀请n对夫妇去参加婚礼. 有一张很长的桌子,人只能坐在桌子的两边,还要满 足下面的要求:1.每对夫妇不能坐在同一侧 2.n对夫妇 之中可能有通奸关系(包括男男,男女,女女) ...