LeetCode 59 Spiral Matrix II (Python)

Posted by 小明MaxMing on December 7, 2020

题目

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

解题思路

根据方向和下一个填充的位置的对应,找到下一个位置,如果这个位置出边界或已经被填过,则转换方向

代码

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        res = [[0] * n for _ in range(n)]
        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        curx = cury = curd = 0
        for i in range(1, n * n + 1):
            res[curx][cury] = i
            newx, newy = curx + directions[curd][0], cury + directions[curd][1]
            if newx < 0 or newx >= n or newy < 0 or newy >= n or res[newx][newy]:
                curd = (curd + 1) % 4
            curx += directions[curd][0]
            cury += directions[curd][1]
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅