LeetCode[Array]: 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[Array]: Spiral Matrix不同的是:这个题目并不适合用递归,因为vector<vector<int>
>
不好递归生成。

可以按照其迭代思路来做,不过这里需要注意的一个技术问题是vector<vector<int>
> matrix
的初始化,一开始我是按照这个方法进行初始化的:

    vector<vector<int> > matrix(n);
    for (int i = 0; i < n; ++i) {
        vector<int> row(n, 0);
        matrix[i] = row;
    }

后来发现C++允许这样完成vector的初始化:vector<vector<int>
> matrix(n, vector<int> (n, 0));

我的C++代码实现如下:

vector<vector<int> > generateMatrix(int n) {
    vector<vector<int> > matrix(n, vector<int> (n, 0));

    int beginRow = 0, endRow = n - 1, beginCol = 0, endCol = n - 1;
    int element = 1;
    while (beginRow <= endRow && beginCol <= endCol) {
        for (int j = beginCol; j <= endCol; ++j)
            matrix[beginRow][j] = (element++);
        if (++beginRow > endRow) break;

        for (int i = beginRow; i <= endRow; ++i)
            matrix[i][endCol] = (element++);
        if (--endCol < beginCol) break;

        for (int j = endCol; j >= beginCol; --j)
            matrix[endRow][j] = (element++);
        if (--endRow < beginRow) break;

        for (int i = endRow; i >= beginRow; --i)
            matrix[i][beginCol] = (element++);
        if (++beginCol > endCol) break;
    }

    return matrix;
}
时间: 2024-10-18 06:38:57

LeetCode[Array]: Spiral Matrix II的相关文章

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】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

[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][JavaScript]Spiral Matrix II

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://leetcode.com/problems/

Java for LeetCode 059 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 ]] 解题思路: 参考Java for LeetCode 054 Spiral Matrix,修改下

[LeetCode][Java] 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 ] ] 题意: 给定一个整数 n,生成一个正方形矩阵.矩阵中包含着从1到n2 这些元素,并且

[C++]LeetCode: 110 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的模型和解法一样.要求我们将1~n^2的数字

leetcode 59 Spiral Matrix II ------ 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 ] ] 这道题其实就是第54题Spiral Matrix的输入和输出反过来而已. 很简单,一圈一圈往里

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 ] ] 题目标签:Array 这道题目和之前的螺旋矩阵几乎没有区别,而且更简单.同样按照螺旋矩阵的特性