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
相关推荐
梦想不只是梦与想11 小时前
python中精度处理:decimal
python·float·精度丢失·decimal·浮点运算
大模型码小白11 小时前
向量化引擎与 AI 排障:当 SIMD 遇到异常检测,存储诊断的范式转移
java·大数据·数据库·人工智能·python
雪的季节12 小时前
【无标题】
linux·服务器·python
奶人五毛拉人一块12 小时前
二分算法以及习题讲解
数据结构·算法·二分
Zachery Pole12 小时前
CCF-CSP备战NO.6【栈】
算法·深度优先
LJHclasstore_luo12 小时前
【C++题解】112354.平行时空
数据结构·c++·算法
sa1002712 小时前
搭建京东评论监控系统,自动捕捉新增评价,快速挖掘用户真实痛点
python
weixin_4617694012 小时前
anaconda安装pytorch安装python
人工智能·pytorch·python
梅雅达编程笔记13 小时前
编程启蒙|Scratch 转 Python 系列第10天:问答闯关游戏实战(AI题库管理+随机出题实战)
人工智能·python·游戏·青少年编程
AOwhisky13 小时前
Python 学习笔记(第十一期)——运维自动化(上·后篇):进程级监控与子进程管理——psutil进阶
运维·开发语言·python·学习·云原生·运维开发