【力扣100】54.螺旋矩阵

添加链接描述

python 复制代码
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. 个人认为这道题的实际意义不大,主要是吓唬人
相关推荐
Kuo-Teng4 小时前
Leetcode438. 找到字符串中所有字母异位词
java·算法·leetcode
墨染点香5 小时前
LeetCode 刷题【138. 随机链表的复制】
算法·leetcode·链表
Shinom1ya_11 小时前
算法 day 41
数据结构·算法·leetcode
一匹电信狗13 小时前
【C++】红黑树详解(2w字详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
仰泳的熊猫14 小时前
LeetCode:72. 超级次方
数据结构·c++·算法·leetcode
_dindong15 小时前
牛客101:递归/回溯
数据结构·c++·笔记·学习·算法·leetcode·深度优先
刃神太酷啦16 小时前
力扣校招算法通关:双指针技巧全场景拆解 —— 从数组操作到环检测的高效解题范式
java·c语言·数据结构·c++·算法·leetcode·职场和发展
Shinom1ya_17 小时前
算法 day 42
数据结构·算法·leetcode
Dylan的码园17 小时前
以二叉树问题为基础的递归调试学习(上)
java·学习·算法·leetcode·r-tree
夏鹏今天学习了吗1 天前
【LeetCode热题100(56/100)】组合总和
算法·leetcode·职场和发展