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一样的分情况,只是这里的分情况是记录在数组里面。利用上,下,左,右,分别记录每次循环可以到达的四个边界,我们每次就输出一环,走完一环往内走。需要注意的:1.要用下标访问vector<vector> >需要先声明大小才可以使用下标访问。2.每走完一环,相应更新四个方向。3.如果是奇数,那么n*n这个数需要另外存。也就是存在[n/2][n/2]的位置。
class Solution {
public:
vector<vector<int> > generateMatrix(int n)
{
    vector<int> sub(n);
    vector<vector<int> > ans(n, sub); // 也可以用vector<vector<int> > ans(n, vector<int>(n));
    if (n < 1) return ans;
    int val = 1, left = 0, right = n-1, up = 1, down = n-1;
    for (int i = 0; i < n/2; i++)
    {
        for (int j = left; j <= right; j++)
        {
            ans[i][j] = val++;
        }
        for (int j = up; j <= down; j++)
        {
            ans[j][right] = val++;
        }
        for (int j = right - 1; j>=left; j--)
        {
            ans[down][j] = val++;
        }
        for (int j = down - 1; j >= up; j--)
        {
            ans[j][left] = val++;
        }
        left++; right--; up++; down--;
    }
    if (n%2)
        ans[n/2][n/2] = n*n;
    return ans;
}
};
时间: 2024-10-05 12:49:52

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

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开始产生连续的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 wh

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,螺旋式的