Leetcode54. 螺旋矩阵

题目传送地址: https://leetcode.cn/problems/spiral-matrix/

运行效率:
在这里插入图片描述
代码如下:

 public static List<Integer> spiralOrder(int[][] matrix) {
        HashSet<String> set = new HashSet<>();
        int count = matrix.length * matrix[0].length;
        int direction = 0;//走的方向   0:从左往右    1:从上往下  2:从右往左  3:从下往上
        List<Integer> list = new ArrayList<>();
        int rowIndex = 0;
        int colIndex = 0;
        while (list.size() < count) {
            int num = matrix[rowIndex][colIndex];
            list.add(num);
            String coordinate = rowIndex + ":" + colIndex;//坐标
            set.add(coordinate);
            // 校验当前坐标的下一个同方向的坐标是否越界
            boolean validNextCoordinate = validNextCoordinate(direction, coordinate, set, matrix);
            //从左往右
            if (direction == 0) {
                if (validNextCoordinate) {  //如果没有越界,那就继续往右边移动
                    colIndex++;
                } else {   //如果越界,则改变方向
                    direction = 1;
                    rowIndex++;  //改变方向,则下一个位置的坐标应该在当前坐标的下方
                }
                continue;
            }
            //从上往下
            if (direction == 1) {
                if (validNextCoordinate) {  //如果没有越界,那就继续往下边移动
                    rowIndex++;
                } else {   //如果越界,则改变方向 那接下来就是要往左走了
                    direction = 2;
                    colIndex--;
                }
                continue;
            }

            //从右往左
            if (direction == 2) {
                if (validNextCoordinate) {  //如果没有越界,那就继续往左边移动
                    colIndex--;
                } else {   //如果越界,则改变方向 那接下来就是要往上走了
                    direction = 3;
                    rowIndex--;
                }
                continue;
            }
            //从下往上
            if (direction == 3) {
                if (validNextCoordinate) {  //如果没有越界,那就继续往上边移动
                    rowIndex--;
                } else {   //如果越界,则改变方向 那接下来就是要往右走了
                    direction = 0;
                    colIndex++;
                }
            }
        }
        return list;
    }

    /**
     * 校验当前坐标的下一个同方向的坐标是否越界
     *
     * @param direction
     * @param coordinate
     * @param set
     * @param matrix
     * @return
     */
    public static boolean validNextCoordinate(Integer direction, String coordinate, HashSet<String> set, int[][] matrix) {
        String[] split = coordinate.split(":"); //当前坐标位置
        int row = Integer.parseInt(split[0]);  //当前行坐标位置
        int col = Integer.parseInt(split[1]);  //当前列坐标位置
        if (direction == 0) {  //如果是从左到右
            col++;   //行坐标不变,列坐标右移
        }
        if (direction == 1) {  //如果是从上到下
            row++;   //列坐标不变,行坐标下移
        }
        if (direction == 2) {  //如果是从右往左
            col--;   //行坐标不变,列坐标左移
        }
        if (direction == 3) {  //如果是从下往上
            row--;   //列坐标不变,行坐标上移
        }
        //首先要确保下一个位置的坐标不能超过matrix的范围吧
        if (row >= matrix.length || row < 0 || col >= matrix[0].length || col < 0) {
            return false;
        }
        //其次下一个位置的坐标不能在set里面
        String nextCoordinate = row + ":" + col;
        return !set.contains(nextCoordinate);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java全栈研发大联盟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值