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解题之前,一定不要看题解,看了就"破功"了!
相关推荐
martian6653 小时前
LeetCode算法领域经典入门题目之“Two Sum”问题
人工智能·算法·leetcode·医学影像
快去睡觉~6 小时前
力扣11:盛水最多的容器
算法·leetcode·职场和发展
夏天ccys10 小时前
LeetCode Day5 -- 栈、队列、堆
算法·leetcode···队列
茴香豆的茴119 小时前
转码刷 LeetCode 笔记[2]:203. 移除链表元素(python)
笔记·leetcode·链表
快去睡觉~1 天前
力扣109:有序链表转换二叉搜索树
算法·leetcode·链表
蒟蒻小袁1 天前
力扣面试150题--阶乘后的零,Pow(x,n)直线上最多的点
leetcode·面试·哈希算法
Q741_1472 天前
如何判断一个数是 2 的幂 / 3 的幂 / 4 的幂 / n 的幂 位运算 总结和思考 每日一题 C++的题解与思路
开发语言·c++·算法·leetcode·位运算·总结思考
我今晚不熬夜2 天前
使用单调栈解决力扣第42题--接雨水
java·数据结构·算法·leetcode
珍珠是蚌的眼泪2 天前
LeetCode_哈希表
leetcode·哈希表·快乐数·字母异位词
flashlight_hi2 天前
LeetCode 分类刷题:209. 长度最小的子数组
javascript·算法·leetcode