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
相关推荐
Swift社区14 分钟前
LeetCode 400 - 第 N 位数字
算法·leetcode·职场和发展
fengfuyao9851 小时前
BCH码编译码仿真与误码率性能分析
算法
哲Zheᗜe༘1 小时前
了解学习Python编程之python基础
开发语言·python·学习
小白不想白a1 小时前
每日手撕算法--哈希映射/链表存储数求和
数据结构·算法
剪一朵云爱着2 小时前
力扣2080. 区间内查询数字的频率
算法·leetcode
落日漫游2 小时前
数据结构笔试核心考点
java·开发语言·算法
麦麦大数据2 小时前
F024 RNN+Vue+Flask电影推荐可视化系统 python flask mysql 深度学习 echarts
python·rnn·深度学习·vue·echarts·电影推荐
workflower2 小时前
Fundamentals of Architectural Styles and patterns
开发语言·算法·django·bug·结对编程
Roc-xb2 小时前
ModuleNotFoundError: No module named ‘conda_token‘
开发语言·python·conda
仰泳的熊猫2 小时前
LeetCode:701. 二叉搜索树中的插入操作
数据结构·c++·算法·leetcode