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
相关推荐
2301_806709386 分钟前
国内知名的生活水箱制造厂有哪些
python·生活
学掌门16 分钟前
超级菜鸟怎么学习数据分析?
python·学习·数据分析
文心快码BaiduComate21 分钟前
光本位联合文心快码打造面向光电芯片研发的全栈AI Agent Lightmate
算法
鸿芯微控科技31 分钟前
压电纳米定位分辨率怎么测?噪声、带宽、最小可检测位移与Python分析
开发语言·python·噪声分析·压电执行器·纳米定位·位移测量
用户77833661321137 分钟前
从 0 搭一个关键词排名监控:核心思路 + 可运行代码
后端·python·api
Fanta丶37 分钟前
14.Python闭包、装饰器
python
一次旅行37 分钟前
ViT/CLIP/LLaVA/GPT-4V/VideoLLM多模态架构全解:原理仿真+工业落地选型+完整推理代码
人工智能·算法·架构
码云骑士1 小时前
99-混合搜索Hybrid-Search-BM25-稀疏稠密向量-融合排序
python
金銀銅鐵1 小时前
[Python] 借助 Turtle 逐字展示《醉翁亭记》
python