LeetCode54题:螺旋矩阵(python3)

路径的长度即为矩阵中的元素数量,当路径的长度达到矩阵中的元素数量时即为完整路径,将该路径返回。

循环打印: "从左向右、从上向下、从右向左、从下向上" 四个方向循环打印。

python 复制代码
class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        if not matrix or not matrix[0]:
            return []
        r,c = len(matrix),len(matrix[0])
        res=[]
        left,right,top,bottom = 0,c-1,0,r-1
        while left<=right and top<=bottom:
            for i in range(left,right+1):
                res.append(matrix[top][i])
            for j in range(top+1,bottom+1):
                res.append(matrix[j][right])
            if left<right and top<bottom:
                for i in range(right-1,left,-1):
                    res.append(matrix[bottom][i])
                for j in range(bottom,top,-1):
                    res.append(matrix[j][left])
            left,right,top,bottom=left+1,right-1,top+1,bottom-1
        return res
相关推荐
计算机毕业编程指导师1 分钟前
【计算机毕设选题】基于Spark的车辆排放分析:2026年热门大数据项目 毕业设计 选题推荐 毕设选题 数据分析 机器学习 数据挖掘
大数据·hadoop·python·计算机·spark·毕业设计·车辆排放
浔川python社7 分钟前
浔川社团关于产品数据情况的官方通告
python
生活很暖很治愈8 分钟前
GUI自动化测试[3]——控件&数鼠标操作
windows·python·功能测试·测试工具
星空露珠19 分钟前
速算24点检测生成核心lua
开发语言·数据库·算法·游戏·lua
老蒋每日coding20 分钟前
Python3基础练习题详解,从入门到熟练的 50 个实例(一)
开发语言·python
HAPPY酷26 分钟前
构建即自由:一份为创造者设计的 Windows C++ 自动化构建指南
开发语言·c++·ide·windows·python·策略模式·visual studio
瑶池酒剑仙27 分钟前
Libvio.link爬虫技术解析大纲
爬虫·python
喵手34 分钟前
Python爬虫实战:构建 Steam 游戏数据库:requests+lxml 实战游戏列表采集与价格监控(附JSON导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集steam商店游戏列表数据·sqlite数据库存放采集数据·价格监控游戏推荐市场分析
happygrilclh38 分钟前
高压高频电源的pid算法
算法
老蒋每日coding43 分钟前
LangGraph:从入门到Multi-Agent超级智能体系统进阶开发
开发语言·python