LeetCode Spiral Matrix II (技巧)

题意:  

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

思路:

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

  迭代:

 1 class Solution {
 2 public:
 3     vector<vector<int> > generateMatrix(int n)
 4     {
 5         vector<vector<int> > ans(n,vector<int>(n));
 6         int cnt=0, i=0;
 7         while(1)
 8         {
 9             if(cnt==n*n)    break;
10             for(int j=i; j<n-i; j++)    ans[i][j]=++cnt;
11             for(int j=i+1; j<n-i; j++)    ans[j][n-i-1]=++cnt;
12             for(int j=n-i-2; j>=i; j--)ans[n-i-1][j]=++cnt;
13             for(int j=n-i-2; j>i; j--)    ans[j][i]=++cnt;
14             i++;
15         }
16         return ans;
17     }
18 };

AC代码

  

  递归:  

 1 class Solution {
 2 public:
 3     vector<vector<int> > ans;
 4     int n;
 5     void generate(int i,int cnt)
 6     {
 7         if(cnt==n*n)    return ;
 8         for(int j=i; j<n-i; j++)    ans[i][j]=++cnt;
 9         for(int j=i+1; j<n-i; j++)    ans[j][n-i-1]=++cnt;
10         for(int j=n-i-2; j>=i; j--)ans[n-i-1][j]=++cnt;
11         for(int j=n-i-2; j>i; j--)    ans[j][i]=++cnt;
12         generate(i+1,cnt);
13     }
14
15
16     vector<vector<int> > generateMatrix(int n)
17     {
18         ans=vector<vector<int> >(n,vector<int>(n));
19         this->n=n;
20         generate(0, 0);
21         return ans;
22     }
23 };

AC代码

时间: 2024-11-10 01:28:51

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

LeetCode: Spiral Matrix II [058]

[题目] 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 ] ] [题意] 给定整数n, 将1,2,3...nxn个数按螺旋旋转的方式填入nxn的矩

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

Spiral Matrix IIGiven 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 ]] SOLUTION 1: 还是与上一题Spiral Matrix类似

leetcode Spiral Matrix II

题目:是Spiral Matrix相关的的.这题的意思是给定一个n,那么在n*n的矩阵里按照循环记录将1,2,3,..., n*n.如下如果给定3,那么: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]一开始我想是不是有数学公式直接下标对应的,那直接遍历输出就可以了.但是推了一会,没有什么好的头绪.于是就分情况讨论了,和Spiral Matrix一样的分情况,只是这里的分情况是记录在数组里面.利用上,下,左,右,分别记录每次循环可以到达的四个边界,我们每次就输

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 ] ] 原题链接:https://oj.leetcode.com/problems/spiral-m

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

[LeetCode]59.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 ] ] [分析] 模拟 [代码] /**-------------------------

【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 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

Spiral Matrix II leetcode java

题目: 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 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

【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 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的