【力扣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. 个人认为这道题的实际意义不大,主要是吓唬人
相关推荐
Hag_207 小时前
LeetCode Hot100 42.接雨水
算法·leetcode·职场和发展
老鼠只爱大米8 小时前
LeetCode经典算法面试题 #153:寻找旋转排序数组中的最小值(暴力搜索、二分查找等五种实现方案详细解析)
算法·leetcode·二分查找·旋转数组·最小值搜索
TracyCoder1238 小时前
LeetCode Hot100(56/100)——131. 分割回文串
算法·leetcode
YGGP9 小时前
【Golang】LeetCode 42. 接雨水
算法·leetcode·职场和发展
@––––––9 小时前
力扣hot100—系列7-二分查找
数据结构·算法·leetcode
鲨鱼吃橘子10 小时前
C++刷题--递归回溯剪枝(二)
开发语言·数据结构·c++·算法·leetcode·深度优先·剪枝
TracyCoder1231 天前
LeetCode Hot100(46/100)——74. 搜索二维矩阵
算法·leetcode·矩阵
im_AMBER1 天前
Leetcode 119 二叉树展开为链表 | 路径总和
数据结构·学习·算法·leetcode·二叉树
苏荷水1 天前
万字总结LeetCode100(持续更新...)
java·算法·leetcode·职场和发展
TracyCoder1231 天前
LeetCode Hot100(50/100)——153. 寻找旋转排序数组中的最小值
算法·leetcode·职场和发展