59. Spiral Matrix II

This is the same problem with https://www.cnblogs.com/feiflytech/p/15862380.html

    public int[][] generateMatrix(int n) {
        int[][] matrix = new int[n][n];
        int top = 0, bottom = n - 1, left = 0, right = n - 1;
        int num = 1;
        int status = 0;
        while (top <= bottom && left <= right) {
            if (status % 4 == 0) {
                for (int j = left; j <= right; j++) {
                    matrix[top][j] = num++;
                }
                top++;
            } else if (status % 4 == 1) {
                for (int i = top; i <= bottom; i++) {
                    matrix[i][right] = num++;
                }
                right--;
            } else if (status % 4 == 2) {
                for (int j = right; j >= left; j--) {
                    matrix[bottom][j] = num++;
                }
                bottom--;
            } else if (status % 4 == 3) {

                for (int i = bottom; i >= top; i--) {
                    matrix[i][left] = num++;
                }
                left++;
            }
            status++;
        }
        return matrix;
    }

 

上一篇:再战leetcode (跳跃游戏 II)


下一篇:直接进入进阶场-palindrome-number(回文数)