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
相关推荐
Pniubi1 分钟前
agent处理封号怎么解决误封问题
开发语言·python
AI技术新视界3 分钟前
Harness^2:递归自进化智能体Harnessing框架
人工智能·深度学习·算法·harness·rsi
这个DBA有点耶13 分钟前
MySQL B-tree索引并发控制优化:写密集场景的锁瓶颈与内核级解法
java·数据库·程序人生·算法·database·dba
爱读书的小胖14 分钟前
Deaf Grandma:C 语言三版代码分析
算法
小羊没烦恼!27 分钟前
浅谈SQL Server中的事务日志(二)----事务日志在修改数据时的角色
前端·数据库·后端·python·web安全·oracle
ss.li34 分钟前
rppg阅读笔记 2026.10.15
图像处理·python·计算机视觉
大貔貅喝啤酒1 小时前
Unittest框架使用教程
自动化测试·python
信奥卷王1 小时前
[GESP202503 六级] 环线(含视频)
算法
一个简单的TTS开发者1 小时前
中文TTS工具有哪些-8款主流方案横向对比
人工智能·后端·python·indextts
CQU_JIAKE1 小时前
10.15【A】
算法