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
相关推荐
进击的松鼠13 分钟前
从对话到动作:用 Function Calling 把 LLM 接到真实 API(含流程拆解)
python·llm·agent
Polar__Star15 分钟前
HTML函数在多GPU系统中如何调用_显卡切换机制说明【汇总】
jvm·数据库·python
2301_8135995523 分钟前
mysql为什么不要在索引列上做运算_mysql函数索引使用场景
jvm·数据库·python
好家伙VCC28 分钟前
**发散创新:基于FFmpeg的视频编码优化实践与实战代码解析**在现代多媒体系统中,
java·python·ffmpeg·音视频
B325帅猫-量子前沿技术研究所32 分钟前
PSD和FFT的关系
人工智能·算法
闻缺陷则喜何志丹33 分钟前
【排序】P6149 [USACO20FEB] Triangles S|普及+
c++·算法·排序·洛谷
人工干智能35 分钟前
科普:CountVectorizer、TF、TF-IDF,三者层层递进
python·tf-idf
avocado_green39 分钟前
【LeetCode】90. 子集 II
算法·leetcode
qq_3422958241 分钟前
如何监控集群 interconnect_ping与traceroute验证心跳通畅
jvm·数据库·python
qq_3422958244 分钟前
Go语言错误处理如何做_Go语言error错误处理教程【实用】
jvm·数据库·python