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
相关推荐
2601_96229827几秒前
Python自动化脚本的编写01
python·appium·自动化脚本·web自动化·应收账款管理
AIFQuant24 分钟前
Python实时外汇行情接入实战:WebSocket与REST K线查询
开发语言·python·websocket
2601_9583529044 分钟前
还要写 AEC 算法?0 代码 + 6 个引脚,F-18 让通话清晰度提升 300%
人工智能·算法·降噪消回音
happyprince1 小时前
03-regmix-深刻观-哲学与升华
算法
babe小鑫1 小时前
生物统计学专业校招:SAS、R、Python学习顺序实用指南
python·学习·r语言
一路向阳~负责的男人1 小时前
运动控制算法收录
算法
czt_java1 小时前
5个核心位运算公式
算法
vx_Biye_Design1 小时前
springboot游泳馆系统93765-计算机课程设计、毕业设计
java·javascript·spring boot·后端·python·spring·课程设计
固定资产管理系统软件1 小时前
该去哪里找专业靠谱的智慧智能设备固定资产管理系统?
人工智能·python
Doubbbbbbble云1 小时前
区间合并问题的常见算法模式与优化思路4
java·数据结构·算法