LeetCode54题:螺旋矩阵(原创)

【题目描述】

给你一个 m 行 n 列的矩阵 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)

【解题代码】

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,矩阵上下左右四个边缘值;
    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. 外面包一层无限循环来螺旋循序访问矩阵数据
    while (true) {
        ...
    }
  3. 尝试向右方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置右侧值加入结果集中,如果已经走到最右侧,那么方向改为向下,并将top值加1;
    if (direction == 0) {
        if (x >= right)
            break;
        result.add(matrix[y][++x]);
        if (x == right) {
            top++;
            direction = 1;
        }
    }
  4. 尝试向下方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置下侧值加入结果集中,如果已经走到最下侧,那么方向改为向左,并将right值减1;
    else if (direction == 1) {
        if (y >= bottom)
            break;
        result.add(matrix[++y][x]);
        if (y == bottom) {
            right--;
            direction = 2;
        }
    } 
  5. 尝试向左方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置左侧值加入结果集中,如果已经走到最左侧,那么方向改为向上,并将bottom值减1;
    else if (direction == 2) {
        if (x <= left)
            break;
        result.add(matrix[y][--x]);
        if (x == left) {
            bottom--;
            direction = 3;
        }
    } 
  6. 尝试向右方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置右侧值,如果已经走到最右侧,那么方向改为向下,并将left值加1;
    else if (direction == 3) {
        if (y <= top)
            break;
        result.add(matrix[--y][x]);
        if (y == top) {
            left++;
            direction = 0;
        }
    }
  7. 返回结果result
    return result;

【思考总结】

  1. 此题关键点在于顺时针螺旋循序就是以右->下->左->上的方式四个方向循环缩小的方式访问矩阵数据,一直到当前方向走不通为止;
  2. 所谓“缩小访问”就是:右方向访问完了,顶部去掉一层,下方向访问完了,右侧去掉一层,左方向访问完了,底部去掉一层,上方向访问完了,左侧去掉一层。
  3. LeetCode解题之前,一定不要看题解,看了就“破功”了!
  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值