题意:  

  从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. lucene底层数据结构——FST,针对field使用列存储,delta encode压缩doc ids数组,LZ4压缩算法

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

  2. python——使用readline库实现tab自动补全

    Input History readline tracks the input history automatically. There are two different sets of funct ...

  3. GridView列的排序功能

    首先要给GridView设置三个属性 GridView4.AllowSorting = true; GridView4.Attributes.Add("SortExpression" ...

  4. placeholder修改颜色

    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #f00; } ::-moz-placeholder { /* Mozilla Fir ...

  5. 使用strace工具故障排查的5种简单方法

    使用strace工具故障排查的5种简单方法 本文源自5 simple ways to troubleshoot using strace strace 是一个非常简单的工具,用来跟踪可执行程序的系统调 ...

  6. php solr 查询

    $options = array( 'hostname' => 'localhost', 'port' => 8080, 'path' => 'solr/test'); $clien ...

  7. 网卡eth0配置信息

    DEVICE=eth0:0 //虚拟网络接口,随意 ONBOOT=yes //系统启动时激活 BOOTPROTO=static //使用静态ip地址 IPADDR=192.168.6.100 //该虚 ...

  8. Disaster Recovery, High Availability, and Continuous Availability - What's the Difference?

    Disaster Recovery, High Availability, and Continuous Availability - What's the Difference? Posted by ...

  9. [开发笔记]-sqlite数据库在使用时遇到的奇葩问题记录

    有时候做些简单的项目一般都会选择sqlite数据库,优点有很多,这里就不详细说了. 在此主要记录一些平时在使用时遇到的问题及解决方法.希望能对大家有所帮助. --------------------- ...

  10. ios应用数据存储的常用方式 ios7.1应用沙盒

    归档:用某种格式保存某个对象,又称持久化. 1XML 属性列表plist归档(持久化) 2Preference(偏好设置) 3NSKeyedArchiver归档 4SQLite3 5Core Data ...