【力扣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. 个人认为这道题的实际意义不大,主要是吓唬人
相关推荐
清铎1 小时前
leetcode_day12_滑动窗口_《绝境求生》
python·算法·leetcode·动态规划
踩坑记录1 小时前
leetcode hot100 42 接雨水 hard 双指针
leetcode
AlenTech3 小时前
207. 课程表 - 力扣(LeetCode)
算法·leetcode·职场和发展
练习时长一年3 小时前
LeetCode热题100(杨辉三角)
算法·leetcode·职场和发展
栈与堆4 小时前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表
努力学算法的蒟蒻4 小时前
day58(1.9)——leetcode面试经典150
算法·leetcode·面试
im_AMBER5 小时前
Leetcode 101 对链表进行插入排序
数据结构·笔记·学习·算法·leetcode·排序算法
踩坑记录5 小时前
leetcode hot100 560.和为 K 的子数组 medium 前缀和 + 哈希表
leetcode
独自破碎E7 小时前
【队列】按之字形顺序打印二叉树
leetcode
AlenTech7 小时前
206. 反转链表 - 力扣(LeetCode)
数据结构·leetcode·链表