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
相关推荐
涵玥15 小时前
python题目练习12-小玉家的电费//小玉在游泳//小鱼的航程
python·python每日一练·洛谷题目
计算机毕业编程指导师15 小时前
基于深度学习的花卉图像分类识别系统(PyTorch + ResNet50 + Django)
pytorch·python·深度学习·分类·毕业设计·迁移学习·图像分类
CC数分15 小时前
统计学基础|先把问题问对:数据分析的六大类型
大数据·人工智能·算法
2601_9628857215 小时前
vnpy 用什么数据源?A 股历史行情接入与 BarData 映射
python
日日大王学编程15 小时前
deerflow-memory-design-blog
python·langchain·开源
地平线开发者16 小时前
CLIP 的训练机制:从对比学习到图文对齐
深度学习·算法·自动驾驶
砚底藏山河16 小时前
量化回测可复现流水线CI化
java·python·ci/cd·金融
磁场转动100万匹16 小时前
道路巡检系统检测案例:YOLOv5 + Flask + PyQt 全栈实现
python·yolo
hetao173383716 小时前
2026-11-02 hetao1733837 的刷题记录
c++·算法
Geektec16 小时前
IMU 选型完全指南:从硬件参数到算法融合的系统工程视角
算法·imu·陀螺仪·加速度计·元器件选型