螺旋矩阵的两种思路及JAVA代码

package arrayandstring;

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

public class SpiralOrder {
    public static void main(String[] args) {
        //测试
        int [][] matrix = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,16}
        };

        List<Integer> res = spiralOrder02(matrix);
        System.out.println(res);
    }
    //思路1:按层模拟,一层一层的遍历
    public static List<Integer> spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return new ArrayList<>();
        }
        int m = matrix.length;//行
        int n = matrix[0].length;//列

        List<Integer> res = new ArrayList<>(m * n);

        int left = 0;
        int right = n - 1;
        int top = 0;
        int bottom = m - 1;
        while (left <= right && top <= bottom) {
            //加入 上
            for (int i = left; i <= right; i ++) {
                res.add(matrix[top][i]);
            }
            //加入 右
            for (int j = top + 1; j <= bottom; j ++) {
                res.add(matrix[j][right]);
            }

            //判断是否需要加入下 左
            if (left < right && top < bottom) {
                //加入下
                for (int i = right - 1; i > left; i --) {
                    res.add(matrix[bottom][i]);
                }
                //加入 左
                for (int j = bottom; j > top; j --) {
                    res.add(matrix[j][top]);
                }
            }
            //进入下一层
            left ++;
            top ++;
            right --;
            bottom --;
        }

        return res;
    }

    //思路2:定义一个和matrix一样的数组,标志元素是否被访问,然后定义一个 转向标志, 循环m * n 次,根据边界条件改变方向
    public static List<Integer> spiralOrder02(int[][] matrix) {

        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return new ArrayList<>();
        }

        int m = matrix.length;//行
        int n = matrix[0].length;//列

        int [][] visited = new int[m][n];//

        List<Integer> res = new ArrayList<>(m * n);
        char dir = 'r';//开始时,向右 r ,然后向下 d ,再向左 l ,最后向上 u,重复
        int r = 0;
        int c = 0;
        for (int i = 0; i < m * n; i ++) {
            if (dir == 'r') {
                res.add(matrix[r][c]);
                visited[r][c] = 1;//已访问
                c ++;
                //改变方向
                if (c == n || ( c < n && visited[r][c] == 1) ) {
                    c --;
                    r ++;
                    dir = 'd';
                }
                continue;
            }

            if (dir == 'd') {
               //开始向下
                res.add(matrix[r][c]);
                visited[r][c] = 1;

                r ++;
                //判断是否改变方向
                if (r == m || ( r < m && visited[r][c] == 1 )) {
                    dir = 'l';
                    c --;
                    r --;
                }
                continue;
            }

            if (dir == 'l') {
                //开始向左
                res.add(matrix[r][c]);
                visited[r][c] = 1;

                c --;
                if (c < 0 || (c >= 0 && visited[r][c] == 1)) {
                    c ++;
                    dir = 'u';
                }

                continue;
            }

            if (dir == 'u') {
                r --;

                res.add(matrix[r][c]);
                visited[r][c] = 1;

                if (r - 1 >= 0 && visited[r - 1][c] == 1) {
                    dir = 'r';
                    c ++;
                }
                continue;
            }

        }

        return res;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值