LeetCode54题:螺旋矩阵(原创)

【题目描述】

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

示例 1:

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

示例 2:

复制代码
输入: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]

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

题目链接. - 力扣(LeetCode)

【解题代码】

java 复制代码
package array.matrix;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SpiralOrder {

    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
        //int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        //int[][] matrix = new int[][]{{3}, {2}};
        List<Integer> result = new SpiralOrder().spiralOrder(matrix);
        System.out.println(Arrays.toString(result.toArray()));
    }

    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();

        int direction = 0;
        int x = -1, y = 0;
        int left = 0, right = matrix[0].length - 1, top = 0, bottom = matrix.length - 1;
        while (true) {
            if (direction == 0) {
                if (x >= right)
                    break;
                result.add(matrix[y][++x]);
                if (x == right) {
                    top++;
                    direction = 1;
                }
            } else if (direction == 1) {
                if (y >= bottom)
                    break;
                result.add(matrix[++y][x]);
                if (y == bottom) {
                    right--;
                    direction = 2;
                }
            } else if (direction == 2) {
                if (x <= left)
                    break;
                result.add(matrix[y][--x]);
                if (x == left) {
                    bottom--;
                    direction = 3;
                }
            } else if (direction == 3) {
                if (y <= top)
                    break;
                result.add(matrix[--y][x]);
                if (y == top) {
                    left++;
                    direction = 0;
                }
            }
        }
        return result;
    }
}

【解题思路】

根据题目描述思考,所谓顺时针螺旋循序就是以右->下->左->上的方式四个方向循环缩小 的方式访问矩阵数据,一直到当前方向走不通为止。循环好理解,而所谓**"缩小访问"**就是:右方向访问完了,顶部去掉一层,下方向访问完了,右侧去掉一层,左方向访问完了,底部去掉一层,上方向访问完了,左侧去掉一层。把握好这一点,代码逻辑就好实现了,按照这个思路,很快完成代码编写,并提交成功

【解题步骤】

  1. 定义变量结果集result,访问方向direction,当前访问位置x,y,矩阵上下左右四个边缘值;

    java 复制代码
    List<Integer> result = new ArrayList<>();
    int direction = 0;
    int x = -1, y = 0;
    int left = 0, right = matrix[0].length - 1, top = 0, bottom = matrix.length - 1;
  2. 外面包一层无限循环来螺旋循序访问矩阵数据

    java 复制代码
    while (true) {
        ...
    }
  3. 尝试向右方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置右侧值加入结果集中,如果已经走到最右侧,那么方向改为向下,并将top值加1;

    java 复制代码
    if (direction == 0) {
        if (x >= right)
            break;
        result.add(matrix[y][++x]);
        if (x == right) {
            top++;
            direction = 1;
        }
    }
  4. 尝试向下方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置下侧值加入结果集中,如果已经走到最下侧,那么方向改为向左,并将right值减1;

    java 复制代码
    else if (direction == 1) {
        if (y >= bottom)
            break;
        result.add(matrix[++y][x]);
        if (y == bottom) {
            right--;
            direction = 2;
        }
    } 
  5. 尝试向左方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置左侧值加入结果集中,如果已经走到最左侧,那么方向改为向上,并将bottom值减1;

    java 复制代码
    else if (direction == 2) {
        if (x <= left)
            break;
        result.add(matrix[y][--x]);
        if (x == left) {
            bottom--;
            direction = 3;
        }
    } 
  6. 尝试向右方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置右侧值,如果已经走到最右侧,那么方向改为向下,并将left值加1;

    java 复制代码
    else if (direction == 3) {
        if (y <= top)
            break;
        result.add(matrix[--y][x]);
        if (y == top) {
            left++;
            direction = 0;
        }
    }
  7. 返回结果result

    java 复制代码
    return result;

【思考总结】

  1. 此题关键点在于顺时针螺旋循序就是以右->下->左->上的方式四个方向循环缩小的方式访问矩阵数据,一直到当前方向走不通为止;
  2. 所谓**"缩小访问"**就是:右方向访问完了,顶部去掉一层,下方向访问完了,右侧去掉一层,左方向访问完了,底部去掉一层,上方向访问完了,左侧去掉一层。
  3. LeetCode解题之前,一定不要看题解,看了就"破功"了!
相关推荐
源代码•宸26 分钟前
Leetcode—721. 账户合并【中等】
c++·经验分享·算法·leetcode·并查集
緈福的街口28 分钟前
【leetcode】77.组合
算法·leetcode·职场和发展
快去睡觉~1 小时前
力扣152:乘积最大子数组
算法·leetcode·职场和发展
程序员Xu1 小时前
【LeetCode热题100道笔记】二叉树的中序遍历
笔记·算法·leetcode
码界奇点2 小时前
豆包新模型矩阵与PromptPilot构建企业级AI开发的体系化解决方案
人工智能·线性代数·ai·语言模型·矩阵·硬件工程
快去睡觉~2 小时前
力扣416:分割等和子集
数据结构·c++·算法·leetcode·职场和发展·动态规划
仙俊红2 小时前
LeetCode每日一题,2025-9-5
算法·leetcode·职场和发展
酸奶乳酪2 小时前
矩阵和向量的双重视角
线性代数·矩阵
阿维的博客日记2 小时前
LeetCode 240: 搜索二维矩阵 II - 算法详解(秒懂系列
算法·leetcode·矩阵
君万4 小时前
【LeetCode每日一题】94. 二叉树的中序遍历 104. 二叉树的最大深度
算法·leetcode·golang