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
相关推荐
心易行者5 分钟前
从零搭建完整Web应用:7步走完全流程,配合web应用托管零门槛上线
人工智能·python·ai编程
小柯南敲键盘23 分钟前
跨境电商图片翻译工具,批量处理视频字幕与抠图
人工智能·python·音视频
2601_9669496531 分钟前
五档盘口数据对量化交易有什么价值?从信号判断到策略风险的完整分析
开发语言·人工智能·python·数据分析·量化·股票数据·quantdash
cxhello42 分钟前
消费者活着、心跳正常、日志干净,但它七天没拉过一条消息
python·kafka
xiaokcehui44 分钟前
地球物理大地测量学计算系列之十七局部重力场SRBF逼近及其性能指标测评
人工智能·算法·机器学习
砚底藏山河1 小时前
【量化纯GET实战 #11】移动均线 MA:5 日 20 日 60 日怎么算怎么用
java·python·金融·maven
小吴学不废Java1 小时前
Python 基础语法
开发语言·python
weixin_433417671 小时前
阿里视频大模型wan2.7‑t2v,可以生成视频
人工智能·python·音视频
sukioe1 小时前
一张图、两把锁:AI 物流履约平台的确定性边界设计
人工智能·python·ai·langchain
Navigator_Z1 小时前
LeetCode //C - 1221. Split a String in Balanced Strings
c语言·算法·leetcode