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
相关推荐
8Qi813 小时前
LeetCode 300 & 674:最长递增子序列 vs 最长连续递增子序列
算法·leetcode·职场和发展·动态规划
sheeta199813 小时前
LeetCode 补拙笔记 日期:2026.06.07 题目:283. 移动零
笔记·算法·leetcode
Irissgwe13 小时前
十、LangGraph能力详解:LangGraph 的其他特性
python·ai·langchain·langgraph
吴阿福|一人公司13 小时前
类变量和实例变量的命名规范有哪些避坑点?
开发语言·python
黎阳之光科技管控13 小时前
纯视觉定位赋能海关口岸 无感通关提升国门安全与效率
算法·安全
zhoupenghui16813 小时前
AI大模型应用部署之Flask框架使用
运维·python·docker·容器·flask·flask框架
ckjoker13 小时前
手敲三Agent串行流水线,我发现了多Agent协作的隐形杀手
python·agent
稷下元歌13 小时前
七天学会plc加机器视觉之AI 接入 外设模块开发全详细操作文档(全程配套视频按文档实操)
python·sql·qt·贪心算法·r语言·wpf·时序数据库
じ☆冷颜〃13 小时前
Picard–Lindelöf定理在CS中的应用:理论框架与算法基础
人工智能·经验分享·笔记·算法·机器学习
不知名的老吴13 小时前
机器学习评价之基础指标
人工智能·算法·机器学习