LeetCode 热题 100 54. 螺旋矩阵

LeetCode 热题 100 | 54. 螺旋矩阵

大家好,今天我们来解决一道经典的算法题------螺旋矩阵。这道题在LeetCode上被标记为中等难度,要求我们按照顺时针螺旋顺序返回矩阵中的所有元素。下面我将详细讲解解题思路,并附上Python代码实现。


问题描述

给定一个 mn 列的矩阵 matrix,请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例1:

plaintext 复制代码
输入: matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出: [1,2,3,6,9,8,7,4,5]

示例2:

plaintext 复制代码
输入: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]

解题思路

核心思想
  1. 边界模拟法

    • 定义矩阵的四个边界:上边界 top、下边界 bottom、左边界 left、右边界 right
    • 按照顺时针方向(右→下→左→上)依次遍历矩阵的边界,并不断调整边界。
  2. 遍历顺序

    • 从左到右遍历上边界,完成后上边界下移。
    • 从上到下遍历右边界,完成后右边界左移。
    • 从右到左遍历下边界,完成后下边界上移。
    • 从下到上遍历左边界,完成后左边界右移。
  3. 终止条件

    • 当所有元素都被遍历时(即 top > bottomleft > right),停止遍历。

Python代码实现

python 复制代码
def spiralOrder(matrix):
    if not matrix:
        return []
    
    top, bottom = 0, len(matrix) - 1
    left, right = 0, len(matrix[0]) - 1
    result = []
    
    while top <= bottom and left <= right:
        # 从左到右遍历上边界
        for i in range(left, right + 1):
            result.append(matrix[top][i])
        top += 1
        
        # 从上到下遍历右边界
        for i in range(top, bottom + 1):
            result.append(matrix[i][right])
        right -= 1
        
        # 检查是否还有下边界需要遍历
        if top <= bottom:
            # 从右到左遍历下边界
            for i in range(right, left - 1, -1):
                result.append(matrix[bottom][i])
            bottom -= 1
        
        # 检查是否还有左边界需要遍历
        if left <= right:
            # 从下到上遍历左边界
            for i in range(bottom, top - 1, -1):
                result.append(matrix[i][left])
            left += 1
    
    return result

# 测试示例
matrix1 = [[1,2,3],[4,5,6],[7,8,9]]
matrix2 = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]

print(spiralOrder(matrix1))  # 输出: [1,2,3,6,9,8,7,4,5]
print(spiralOrder(matrix2))  # 输出: [1,2,3,4,8,12,11,10,9,5,6,7]

代码解析

  1. 初始化边界

    • topbottom 分别表示矩阵的上下边界。
    • leftright 分别表示矩阵的左右边界。
  2. 顺时针遍历

    • 从左到右 :遍历上边界,完成后将 top 下移。
    • 从上到下 :遍历右边界,完成后将 right 左移。
    • 从右到左 :遍历下边界(需检查 top <= bottom),完成后将 bottom 上移。
    • 从下到上 :遍历左边界(需检查 left <= right),完成后将 left 右移。
  3. 终止条件

    • top > bottomleft > right 时,说明所有元素已被遍历。

复杂度分析

  • 时间复杂度 :O(m × n),其中 m 是矩阵的行数,n 是矩阵的列数。我们需要遍历矩阵中的每个元素一次。
  • 空间复杂度:O(1),除了输出结果外,只使用了常数个额外空间。

示例运行

示例1
plaintext 复制代码
输入: matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出: [1,2,3,6,9,8,7,4,5]
示例2
plaintext 复制代码
输入: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]

进阶:其他解法

方法一:递归法
python 复制代码
def spiralOrder_recursive(matrix):
    if not matrix:
        return []
    
    result = []
    rows, cols = len(matrix), len(matrix[0])
    
    def helper(top, bottom, left, right):
        if top > bottom or left > right:
            return
        
        # 从左到右遍历上边界
        for i in range(left, right + 1):
            result.append(matrix[top][i])
        top += 1
        
        # 从上到下遍历右边界
        for i in range(top, bottom + 1):
            result.append(matrix[i][right])
        right -= 1
        
        # 检查是否还有下边界需要遍历
        if top <= bottom:
            # 从右到左遍历下边界
            for i in range(right, left - 1, -1):
                result.append(matrix[bottom][i])
            bottom -= 1
        
        # 检查是否还有左边界需要遍历
        if left <= right:
            # 从下到上遍历左边界
            for i in range(bottom, top - 1, -1):
                result.append(matrix[i][left])
            left += 1
        
        helper(top, bottom, left, right)
    
    helper(0, rows - 1, 0, cols - 1)
    return result
  • 时间复杂度:O(m × n)
  • 空间复杂度:O(min(m, n)),递归调用的栈空间。

总结

通过使用边界模拟法,我们可以高效地按照顺时针螺旋顺序遍历矩阵中的所有元素。这种方法直观且易于实现,适合大多数场景。希望这篇题解对大家有所帮助,如果有任何问题,欢迎在评论区留言讨论!

关注我,获取更多算法题解和编程技巧!

相关推荐
夏鹏今天学习了吗2 小时前
【LeetCode热题100(82/100)】单词拆分
算法·leetcode·职场和发展
mit6.8243 小时前
mysql exe
算法
2501_901147833 小时前
动态规划在整除子集问题中的应用与高性能实现分析
算法·职场和发展·动态规划
中草药z3 小时前
【嵌入模型】概念、应用与两大 AI 开源社区(Hugging Face / 魔塔)
人工智能·算法·机器学习·数据集·向量·嵌入模型
踩坑记录4 小时前
leetcode hot100 189.轮转数组 medium
leetcode
知乎的哥廷根数学学派4 小时前
基于数据驱动的自适应正交小波基优化算法(Python)
开发语言·网络·人工智能·pytorch·python·深度学习·算法
ADI_OP4 小时前
ADAU1452的开发教程10:逻辑算法模块
算法·adi dsp中文资料·adi dsp·adi音频dsp·adi dsp开发教程·sigmadsp的开发详解
xingzhemengyou14 小时前
C语言 查找一个字符在字符串中第i次出现的位置
c语言·算法
Dream it possible!5 小时前
LeetCode 面试经典 150_二分查找_在排序数组中查找元素的第一个和最后一个位置(115_34_C++_中等)
c++·leetcode·面试
小六子成长记6 小时前
【C++】:搜索二叉树的模拟实现
数据结构·c++·算法