Leetcode 59. 螺旋矩阵 II(Spiral Matrix II)

这是我的第一篇博客,先随便写写,后续会补充更多内容。

注:本文表示数组位置(index)都是从下标为0开始。

题目描述:

给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

解法:

这道题的重点在于找到数组相应位置(index)迭代的关系,观察下面这个例子:

用 i 表示行, j 表示列, n 表示总行数(列数)

[
 [   1,    2,  3, 4 ],
 [ 12, 13, 14, 5 ],
 [ 11, 16, 15, 6 ], [ 10,   9,   8, 7 ],]

从第一行第一个位置(0, 0)到(0, 3),各个位置的行数一致,列数在不断增加;当第一行被填满时,剩下的数字会从最后一列第一个位置(0,3)开始往下存放数字,直到到达最后一个位置(3, 3)。在这个过程中,列数是不变的,行数一直在增加,直到到达位置(3, 3);接下来类似,从位置(3, 3)到位置(3, 0),行数不变而列数在减少;最后在位置(3, 3)到位置(1, 0),列数不变而行数减少。
可以发现1-12迭代的过程和13-16迭代的过程是类似的,都是从头开始一直迭代到初始位置的下方。
[
 [  1,  2,  3,  4 ],
 [ 12,           5 ],
 [ 11,           6 ], [ 10,  9,  8, 7 ],]
[
 [ 13, 14 ],
 [ 16, 15 ],]

将1-12迭代过程中发生变化的四个临界点找出(用代数式表示):(0, n-1), (n-1, n-1), (n-1, 0), (1, 0)13-16迭代的临界点:(1, n-2), (n-2, n-2), (n-2, 1), (2, 1)容易找到下面这个关系:h = 0;(0, n-h-1), (n-h-1, n-h-1), (n-h-1, 0), (h+1, h)当到达点(h+1, h)时,h++(因为要从内部开始新一轮迭代),并且i,j的递增模式应该恢复到原始状态,即i不变,j递增。能想到这后面的代码就很容易了,只需要迭代生成n*n个数字,分别存储到相应的数组位置上就可以了。下面是Python实现的代码:
def generateMatrix(n):
    res = [[0 for i in range(n)] for i in range(n)]
    i, j, flag = 0, 0, 0
    h = 0
    for k in range(1, n*n+1):
        if j == n-h-1 and i == h:
            flag = 1
        elif i == n-h-1 and j == n-h-1:
            flag = 2
        elif i == n-h-1 and j == h:
            flag = 3
        elif i == h+1 and j == h:
            h += 1
            flag = 0
        res[i][j] = k
        if flag == 0:
            j += 1
        elif flag == 1:
            i += 1
        elif flag == 2:
            j -= 1
        elif flag == 3:
            i -= 1
    return res

原文地址:https://www.cnblogs.com/Alevals/p/11563398.html

时间: 2024-07-31 14:29:55

Leetcode 59. 螺旋矩阵 II(Spiral Matrix II)的相关文章

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

[Swift]LeetCode59. 螺旋矩阵 II | Spiral Matrix II

Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ],

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

每日算法之四十一: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 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 all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input:

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

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想法也是类似的,就是依照矩阵从外圈到内圈建立