LeetCode 59. Spiral Matrix II

简介: 给定正整数n,以螺旋顺序生成填充有从1到n2的元素的方阵。

Description



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的元素的方阵。


思路



  • 此题目同第48题Rotate Image第54题Spiral Matrix做法类似.第48题解析在这里,第54题解析在这里.
  • 同样,我们维护四个指针,rowtop:指向第一行,rowbot:指向最后一行,coleft:指向第一列,colright:指向最后一列。我们另外维护一个num用来记录当前位置填的值.


class Solution:
    def generateMatrix(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        num = 1
        # 初始化结果数组,每个位置都初始化为0
        res = [[0 for _ in range(n)] for _ in range(n)]
        rowtop, coleft, rowbot, colright = 0, 0, n-1, n-1
        while rowbot > rowtop and coleft < colright:
            # 填第一行
            for i in range(coleft, colright):
                res[rowtop][i] = num
                num += 1
            # 填最后一列
            for i in range(rowtop, rowbot):
                res[i][colright] = num
                num += 1
            # 填最后一列
            for i in range(colright, coleft, -1):
                res[rowbot][i] = num
                num += 1
            # 填第一列
            for i in range(rowbot, rowtop, -1):
                res[i][coleft] = num
                num += 1
            # 进入下一层
            rowtop += 1
            rowbot -= 1
            coleft += 1
            colright -= 1
        # 如果是奇数行,则最后一行需要单独填写
        if n % 2:
            for i in range(coleft, colright+1):
                res[rowtop][i] = num
                num += 1
        return res


源代码文件在这里.

目录
相关文章
|
7月前
Leetcode 74. Search a 2D Matrix
这道题很简单,为此专门写篇博客其实算博客凑数了。给你一个每一行每一列都是增序,且每一行第一个数都大于上一行末尾数的矩阵,让你判断某个数在这个矩阵中是否存在。 假设矩阵是m*n,扫一遍的时间复杂度就是O(m*n),题目中给出的这么特殊的矩阵,时间复杂度可以降到O(m+n),具体代码如下,写的比较挫。
65 1
|
7月前
Leetcode 240. Search a 2D Matrix II
具体思路就是每一行倒着扫,扫到第一个比target小的数就跳到下行,如果等于当然是直接返回true了,如果下一行还比target小就继续跳下一行,直到最后一行。 为啥这么做是可行的? 可能我比较笨,想了半天才想到。 因为每一列都是增序的,举个例子,假设matrix[0][5] > target,那么[0][5]位置右下(包含右和下)所有元素不可能比target小。
33 0
|
Python
LeetCode 378. Kth S Element in a Sorted Matrix
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。 请注意,它是排序后的第k小元素,而不是第k个元素。
83 0
LeetCode 378. Kth S Element in a Sorted Matrix
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
52 0
LeetCode 329. Longest Increasing Path in a Matrix
|
算法
LeetCode 240. Search a 2D Matrix II
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性: 每行的元素从左到右升序排列。 每列的元素从上到下升序排列。
63 0
LeetCode 240. Search a 2D Matrix II
|
算法
LeetCode 74. Search a 2D Matrix
编写一个有效的算法,搜索m×n矩阵中的值。 此矩阵具有以下属性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.
59 0
LeetCode 74. Search a 2D Matrix
|
存储 索引
LeetCode 73. Set Matrix Zeroes
给定一个m * n 的矩阵,如果当前元是0,则把此元素所在的行,列全部置为0. 额外要求:是否可以做到空间复杂度O(1)?
84 0
LeetCode 73. Set Matrix Zeroes
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
|
14天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-2
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题