每日算法之四十一: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 ]
]

针对这个问题采用最直观的方式即可,即螺旋插入,这里有两个地方需要注意,一个是插入边界的界定,另一个就是辅助矩阵的开辟。

第一 个问题我们可以采用举例的方式,画一个4*4的矩阵,1-12这些数字插入进去是螺旋的一周,用数字标记插入的范围,再转化为和行数、矩阵规模n相关的表达式即可。

第二个问题使用O(n*n)的空间复杂度。

代码如下:

class Solution {
public:
    vector<vector<int> > generateMatrix(int n) {
        vector<vector<int> > matrix;
        if(n < 0)
            return matrix;
        int **m = new int*[n];
        for(int i = 0; i < n; i++)
        {
            m[i] = new int[n];
            memset(m[i], 0, sizeof(int)*n);
        }
        int currentnum = 1;
        int loop = 0;
        while(loop < (n+1)/2)
        {
            int tmp = n - 1 - loop;
            for(int i = loop; i <= tmp; i++)
                m[loop][i] = currentnum++;
            for(int i = loop + 1; i <= tmp; i++)
                m[i][tmp] = currentnum++;
            for(int i = tmp - 1; i >= loop; i--)
                m[tmp][i] = currentnum++;
            for(int i = tmp - 1; i >= loop + 1; i--)
                m[i][loop] = currentnum++;
            loop++;
        }
        for(int i = 0; i < n; i++)
        {
            vector<int> tmp;
            for(int j = 0; j < n; j++)
                tmp.push_back(m[i][j]);
            matrix.push_back(tmp);
        }
        for(int i = 0; i < n; i++)
            delete[] m[i];
        delete[] m;
        return matrix;
    }
};

每日算法之四十一:Spiral Matrix II (螺旋矩阵),布布扣,bubuko.com

时间: 2024-10-12 13:04:27

每日算法之四十一:Spiral Matrix II (螺旋矩阵)的相关文章

leetCode 59.Spiral Matrix II (螺旋矩阵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 ] ] 思路:此题和螺旋矩阵题差不多,没什么难的地方,主要就是四个方向的转换. 具体代码如下: pu

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 螺旋矩阵2之python大法好,四行就搞定,你敢信?

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] 885. Spiral Matrix III 螺旋矩阵之三

On a 2 dimensional grid with?R?rows and?C?columns, we start at?(r0, c0)?facing east. Here, the north-west corner of the grid is at the?first row and column, and the south-east corner of the grid is at the last row and column. Now, we walk in a clockw

[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 OJ:Spiral Matrix(螺旋矩阵)

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 典型的dfs问题,与前面一个问题比较像,代码如下

每日算法之四十四:Unique Path(矩阵中不重复路径的数目)

Unique Paths: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked

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 解题报告-三种方法解决旋转矩阵问题

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类似