【力扣100】54.螺旋矩阵

添加链接描述

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        if not matrix or not matrix[0]:
            return list()
        
        rows, columns = len(matrix), len(matrix[0])
        order = list()
        left, right, top, bottom = 0, columns - 1, 0, rows - 1
        while left <= right and top <= bottom:
            for column in range(left, right + 1):
                order.append(matrix[top][column])
            for row in range(top + 1, bottom + 1):
                order.append(matrix[row][right])
            if left < right and top < bottom:
                for column in range(right - 1, left, -1):
                    order.append(matrix[bottom][column])
                for row in range(bottom, top, -1):
                    order.append(matrix[row][left])
            left, right, top, bottom = left + 1, right - 1, top + 1, bottom - 1
        return order

思路:

  1. 按层进行遍历
  2. 然后就是判断每个边界值的条件
  3. 向左和下走是被允许的,向右或向上走是不被允许的需要条件判断
  4. 个人认为这道题的实际意义不大,主要是吓唬人

相关推荐

  1. 59-螺旋矩阵

    2023-12-13 17:42:02       68 阅读
  2. 54. 螺旋矩阵

    2023-12-13 17:42:02       68 阅读
  3. 100】54.螺旋矩阵

    2023-12-13 17:42:02       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-13 17:42:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-13 17:42:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-13 17:42:02       82 阅读
  4. Python语言-面向对象

    2023-12-13 17:42:02       91 阅读

热门阅读

  1. 微服务组件Nacos的学习(1)

    2023-12-13 17:42:02       64 阅读
  2. 【密码学】RSA破解方法汇总(PYTHON实现)

    2023-12-13 17:42:02       61 阅读
  3. WebSocket实现数据的实时推送

    2023-12-13 17:42:02       56 阅读
  4. 杨辉三角(Python)

    2023-12-13 17:42:02       60 阅读
  5. 前端优化 ----防抖 节流

    2023-12-13 17:42:02       60 阅读
  6. Vue mixins详解

    2023-12-13 17:42:02       52 阅读
  7. 索引的使用

    2023-12-13 17:42:02       45 阅读