点击这里给我发邮件
点击这里给我发消息

记一道螺旋矩阵顺序输出题

输入一个螺旋矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字:

示例:

1、

 

输出: [1, 2, 3, 4, 5, 6, 7, 8, 9]

2、

 

 

输出:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

3、

 

 

输出:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

 

题目分析:

这个题目的要求就是打印一个环,这个环是从外到内。

总结:这个题目的关键点就是找到四个方便变化的状态和终止遍历的条件。

参考代码:

public static void main(String[] args) {
        int[][] arr = {
                {1, 2, 3},
                {8, 9, 4},
                {7, 6, 5}
        };
//        int[][] arr = {{1, 2, 3, 4}, {12, 13, 14, 5}, {11, 16, 15, 6}, {10, 9, 8, 7}};
//        int[][] arr = {
//                {1, 2, 3, 4, 5},
//                {16, 17, 18, 19, 6},
//                {15, 24, 25, 20, 7},
//                {14, 23, 22, 21, 8},
//                {13, 12, 11, 10, 9}
//        };


        int[] a = spiralMatrix(arr);
        System.out.println(Arrays.toString(a));
    }

    public static int[] spiralMatrix(int[][] arr) {

        int rightIndex = 0;
        int length = arr.length * arr[0].length;
        int a[] = new int[length];
        int[] position = {0, 0};
        int direction = 0;
        int losex = 0;
        int losey = 0;
        int xstep = 0;
        int ystep = 0;

        int loopIndex = 0;

        while (rightIndex < length) {
            if (loopIndex++ > length) {
                break;
            }
            int xlength = arr[position[1]].length - losex;
            int ylength = arr.length - losey;

            System.out.println(" rightIndex:" + rightIndex + "=>  x:" + position[1] + ", y:" + position[0] + ", direction:" + direction);

            a[rightIndex++] = arr[position[0]][position[1]];

            switch (direction) {
                case 1:
                    if (++xstep < ylength) {
                        position[0]++;
                    } else {
                        direction = 2;
                        position[1]--;
                        losex++;
                        xstep = 0;
                        ystep = 0;
                    }
                    break;
                case 2:
                    if (++ystep < xlength) {
                        position[1]--;
                    } else {
                        direction = 3;
                        position[0]--;
                        losey++;
                        xstep = 0;
                        ystep = 0;
                    }
                    break;
                case 3:
                    if (++xstep < ylength) {
                        position[0]--;
                    } else {
                        direction = 0;
                        position[1]++;
                        losex++;
                        xstep = 0;
                        ystep = 0;
                    }
                    break;
                default:
                    if (++ystep < xlength) {
                        position[1]++;  // x++
                    } else {
                        direction = 1;
                        position[0]++; // y++
                        losey++;
                        xstep = 0;
                        ystep = 0;
                    }
                    break;
            }
        }
        return a;

    }

 

posted on 2020-10-19 15:26  菜风少侠  阅读(228)  评论(0编辑  收藏  举报