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解题之前,一定不要看题解,看了就"破功"了!
相关推荐
孙小二写代码41 分钟前
[leetcode刷题]面试经典150题之1合并两个有序数组(简单)
算法·leetcode·面试
David猪大卫1 小时前
数据结构修炼——顺序表和链表的区别与联系
c语言·数据结构·学习·算法·leetcode·链表·蓝桥杯
MogulNemenis3 小时前
力扣春招100题——队列
数据结构·算法·leetcode
是小Y啦4 小时前
leetcode 106.从中序与后续遍历序列构造二叉树
数据结构·算法·leetcode
程序猿练习生5 小时前
C++速通LeetCode中等第9题-合并区间
开发语言·c++·leetcode
liuyang-neu5 小时前
力扣 42.接雨水
java·算法·leetcode
Ddddddd_1585 小时前
C++ | Leetcode C++题解之第416题分割等和子集
c++·leetcode·题解
凌肖战6 小时前
力扣上刷题之C语言实现(数组)
c语言·算法·leetcode
Milo_K7 小时前
今日 leetCode 15.三数之和
算法·leetcode
Darling_007 小时前
LeetCode_sql_day28(1767.寻找没有被执行的任务对)
sql·算法·leetcode