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
相关推荐
打工仔折腾 AI16 分钟前
智能体开发实战:Agent架构拆解与ReAct执行循环工程落地
前端·后端·python·react.js·架构·langchain·ai agent 实战
海边捡石子32 分钟前
用 Nuitka 保护 Python 核心业务源码
linux·后端·python
jiecy34 分钟前
用AI做了个吉他调音器节奏器
python·ai编程
呆呆虫1244 分钟前
批量采 Google Videos 视频搜索与元数据:SerpBase 等 5 种接口方案对比
python·serp api·serpbase·视频元数据·google videos
yuki蜜语1 小时前
给大模型配一个"物理传感器":用肚脐椭圆检测,给中医 RAG 加一道可反驳的校验层
算法·计算机视觉·canvas
啃鸭头1 小时前
RAG实战:S3 加来源背书,治复述抬分 + 格式规则换个写法再验
python
陈如水1 小时前
尚硅谷 Spring AI学习
人工智能·python·spring
背包客(wyq)1 小时前
第14讲 复合数据结构:元组与字典
python
是菜鸡小小陈啊1 小时前
【CS329Z Assignment 1】从零构建 LLM Agent Harness(学习笔记)06
人工智能·笔记·python·学习·llm·agent
AAA修煤气灶刘哥1 小时前
明明搜到了同一个故障码,RAG 为什么还会教错你?
算法·llm·agent