leetcode刷题(剑指offer)54.螺旋矩阵

54.螺旋矩阵

给你一个 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

题解

本题使用了vis数组记录了走过的路径,设定初始移动方向为向右,如果不能再向右了那就切换方向为向下移动,判定条件为(当前格子已经走过了,或者数组越界了)。按照上述逻辑依次切换四个方向,当走完整个matrix的时候,vis会全部记录为true,此时,上下左右都无法移动,可以直接将结果返回。

代码实现如下:

package com.offer;


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

public class _54螺旋矩阵 {

    public static boolean[][] vis;

    public static void main(String[] args) {
        int[][] matrix = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        };
        System.out.println(spiralOrder(matrix));
    }

    public static List<Integer> spiralOrder(int[][] matrix) {
        vis = new boolean[matrix.length][matrix[0].length];
        List<Integer> res = new ArrayList<>();
        // 0: 表示向右,1表示向下,2表示向左,3表示向上
        int direction = 0;
        int row = 0;
        int col = 0;
        int rowLen = matrix.length;
        int colLen = matrix[0].length;
        while (true) {
            res.add(matrix[row][col]);
            vis[row][col] = true;
            // 设置下一步的方向
            if (direction == 0) {
                // 当前是向左的,尝试向左走一步,如果不能走将方向改为向下
                if (!canMove(row, col + 1, rowLen, colLen)) {
                    direction = 1;
                }
            } else if (direction == 1) {
                // 当前是向下的,尝试向下走一步,如果不能走将方向改为向右
                if (!canMove(row + 1, col, rowLen, colLen)) {
                    direction = 2;
                }
            } else if (direction == 2) {
                // 当前是向右的,尝试向右走一步,如果不能走将方向改为向上
                if (!canMove(row, col - 1, rowLen, colLen)) {
                    direction = 3;
                }
            } else {
                // 当前是向上的,尝试向上走一步,如果不能走将方向改为向右
                if (!canMove(row - 1, col, rowLen, colLen)) {
                    direction = 0;
                }
            }

            // 如果四个方向都不能走,说明已经结束了,直接退出程序
            if (!canMove(row, col + 1, rowLen, colLen) &&
                !canMove(row + 1, col, rowLen, colLen) &&
                !canMove(row, col - 1, rowLen, colLen) &&
                !canMove(row - 1, col, rowLen, colLen)) {
                return res;
            }

            if (direction == 0) {
                col++;
            } else if (direction == 1) {
                row++;
            } else if (direction == 2) {
                col--;
            } else {
                row--;
            }
        }
    }

    public static boolean canMove(int row, int col, int rowLen, int colLen) {
        if (row >= 0 && row < rowLen && col >= 0 && col < colLen && !vis[row][col]) {
            return true;
        }
        return false;
    }
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值