【LeetCode Hot100】螺旋矩阵|边界模拟,Java实现,图解+代码

💻 [LeetCode Hot100] 螺旋矩阵|边界模拟,Java实现!图解+代码

✏️本文对应题目链接:螺旋矩阵


📌 题目描述

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

示例:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]
解释:顺时针螺旋顺序遍历矩阵。

🧠 解题思路(图文分解)

❗ 核心难点

如何模拟顺时针螺旋遍历矩阵的边界?


方法一:边界模拟法(黄金思路)✨

关键步骤:

  1. 定义边界
    • leftrighttopbottom 分别表示当前遍历的左右上下边界
  2. 模拟遍历
    • 从左到右遍历上边界
    • 从上到下遍历右边界
    • 从右到左遍历下边界
    • 从下到上遍历左边界
  3. 更新边界:每次遍历完一个边界后,缩小边界范围
  4. 终止条件:当边界范围无效时停止遍历

图解边界模拟

输入矩阵:

matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]

步骤1:初始化边界

left = 0, right = 2
top = 0, bottom = 2

步骤2:模拟遍历

- 从左到右遍历上边界 → [1,2,3]
- 从上到下遍历右边界 → [6,9]
- 从右到左遍历下边界 → [8,7]
- 从下到上遍历左边界 → [4]

步骤3:更新边界

left = 1, right = 1
top = 1, bottom = 1

步骤4:继续遍历

- 从左到右遍历上边界 → [5]

最终结果:

[1,2,3,6,9,8,7,4,5]

🚀 代码实现

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        if (matrix == null || matrix.length == 0) {
            return result;
        }
        
        int left = 0, right = matrix[0].length - 1;
        int top = 0, bottom = matrix.length - 1;
        
        while (left <= right && top <= bottom) {
            // 从左到右遍历上边界
            for (int i = left; i <= right; i++) {
                result.add(matrix[top][i]);
            }
            top++;
            
            // 从上到下遍历右边界
            for (int i = top; i <= bottom; i++) {
                result.add(matrix[i][right]);
            }
            right--;
            
            // 从右到左遍历下边界
            if (top <= bottom) {
                for (int i = right; i >= left; i--) {
                    result.add(matrix[bottom][i]);
                }
                bottom--;
            }
            
            // 从下到上遍历左边界
            if (left <= right) {
                for (int i = bottom; i >= top; i--) {
                    result.add(matrix[i][left]);
                }
                left++;
            }
        }
        
        return result;
    }
}

💡 复杂度分析

  • 时间复杂度:O(m * n) → 遍历矩阵中的每个元素一次
  • 空间复杂度:O(1) → 仅用常数空间(结果列表不计入空间复杂度)

方法二:方向模拟法(进阶思路)

关键思路:使用方向数组模拟螺旋遍历,适合复杂路径问题。

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        if (matrix == null || matrix.length == 0) {
            return result;
        }
        
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // 右、下、左、上
        int m = matrix.length, n = matrix[0].length;
        boolean[][] visited = new boolean[m][n];
        int directionIndex = 0;
        int row = 0, col = 0;
        
        for (int i = 0; i < m * n; i++) {
            result.add(matrix[row][col]);
            visited[row][col] = true;
            int nextRow = row + directions[directionIndex][0];
            int nextCol = col + directions[directionIndex][1];
            if (nextRow < 0 || nextRow >= m || nextCol < 0 || nextCol >= n || visited[nextRow][nextCol]) {
                directionIndex = (directionIndex + 1) % 4; // 改变方向
            }
            row += directions[directionIndex][0];
            col += directions[directionIndex][1];
        }
        
        return result;
    }
}

🌟 总结要点

边界模拟核心思想:通过缩小边界模拟螺旋遍历
方向模拟法核心:利用方向数组处理复杂路径问题
适用场景:矩阵遍历、路径模拟


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值