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
相关推荐
贝猫说python12 小时前
阿里云部署qwen 大模型从0-1,第3步:阿里云服务器上部署大模型
python
橘子汽水16812 小时前
Leetcode 763,45 划分字母区间 跳跃游戏II
数据结构·算法·leetcode
少陽君12 小时前
服务器服务检查报告
python
x秀x12 小时前
sam3新手使用教程
开发语言·python
Java后端的Ai之路12 小时前
大模型LLM评估完全指南
开发语言·人工智能·python·llm·评估
外收内放12 小时前
Python学习记录25(基础知识终结篇)
python·学习
别动我齐刘海12 小时前
ROS2 Jazzy + C++ 实战路线——基础学习1
c++·vscode·python·ubuntu·机器学习·自动驾驶·github
life码农12 小时前
python框架Flask多语言配置示例
python·flask
苏离~Hack13 小时前
SL-crawler:一个可视化配置的通用网页与 API 数据采集工具
python
小白学大数据13 小时前
Python 小工具实战:用问财接口搭建金融数据采集器
开发语言·人工智能·python