• 【LeetCode】59. 螺旋矩阵 II


    1 问题

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

    示例 1:
    在这里插入图片描述
    输入:n = 3
    输出:[[1,2,3],[8,9,4],[7,6,5]]

    示例 2:

    输入:n = 1
    输出:[[1]]

    2 答案

    自己写的,参考54.螺旋矩阵

    class Solution:
        def generateMatrix(self, n: int) -> List[List[int]]:
            matrix = [[0 for _ in range(n)] for _ in range(n)]
            up, down, left, right = 0, n-1, 0, n-1
            x, y, cur_d = 0, 0, 0
            dire = [[0, 1], [1, 0], [0, -1], [-1, 0]]
            for i in range(n**2):
                matrix[x][y] = i+1
                if cur_d == 0 and y == right:
                    cur_d += 1
                    up += 1
                if cur_d == 1 and x == down:
                    cur_d += 1
                    right -= 1
                if cur_d == 2 and y == left:
                    cur_d += 1
                    down -= 1
                if cur_d == 3 and x == up:
                    cur_d += 1
                    left += 1
                cur_d %= 4
                x += dire[cur_d][0]
                y += dire[cur_d][1]
            return matrix
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述
    官方解,循环用的while,方法相似

    class Solution(object):
        def generateMatrix(self, n):
            if n == 0: return []
            res = [[0] * n for i in range(n)]
            left, right, up, down = 0, n - 1, 0, n - 1
            x, y = 0, 0
            dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
            cur_d = 0
            count = 0
            while count != n * n:
                res[x][y] = count + 1
                count += 1
                if cur_d == 0 and y == right:
                    cur_d += 1
                    up += 1
                elif cur_d == 1 and x == down:
                    cur_d += 1
                    right -= 1
                elif cur_d == 2 and y == left:
                    cur_d += 1
                    down -= 1
                elif cur_d == 3 and x == up:
                    cur_d += 1
                    left += 1
                cur_d %= 4
                x += dirs[cur_d][0]
                y += dirs[cur_d][1]
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    也可以利用坐标是否超过边界来变换遍历方向

    class Solution(object):
        def generateMatrix(self, n):
            directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
            res = [[0] * n for i in range(n)]
            x, y = 0, 0
            count = 0
            cur_d = 0
            while count != n * n:
                res[x][y] = count + 1
                count += 1
                dx, dy = directions[cur_d][0], directions[cur_d][1]
                newx, newy = x + dx, y + dy  # newx, newy 用于试错,看坐标是否超过边界
                if newx < 0 or newx >= n or newy < 0 or newy >= n or res[newx][newy] != 0:
                    cur_d = (cur_d + 1) % 4
                    dx, dy = directions[cur_d][0], directions[cur_d][1]
                x, y = x + dx, y + dy
            return res
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    https://leetcode.cn/problems/spiral-matrix-ii/solutions/659234/ju-zhen-bian-li-wen-ti-de-si-bu-qu-by-fu-sr5c/

  • 相关阅读:
    C#图片批量下载Demo
    Python实用模块之argparse
    基于quartz的定时任务动态启停实现分析(人人平台为例)
    【路径规划】基于梯度下降算法求解自定义起点终点障碍路径规划问题附matlab代码
    rust所有权
    2022-8-21 第七小组 学习日记 (day45)Java测试***
    华为OD机试真题- 阿里巴巴找黄金宝箱(IV)-2023年OD统一考试(B卷)
    ARC113D题解
    Ruby on Rails Post项目设置网站初始界面
    从0开始python学习-33.夹具@pytest.fixture(scope=““,params=““,autouse=““,ids=““,name=““)
  • 原文地址:https://blog.csdn.net/CSDNLHCC/article/details/133954685