给定一个正整数 n,生成一个包含 1 到n 2 n^2n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

输入: 3
输出:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]
//分别对每个边界进行赋值
class Solution {
    //假设3*3矩阵
    public int[][] generateMatrix(int n) {
        //左右上下边界
        int l = 0, r = n - 1, t = 0, b = n - 1;
        int[][] mat = new int[n][n];
        //tar 是总数 
        int num = 1, tar = n * n;

        while(num <= tar){
            for(int i = l; i <= r; i++) mat[t][i] = num++; // left to right.
            	t++;
            for(int i = t; i <= b; i++) mat[i][r] = num++; // top to bottom.
            	r--;
            for(int i = r; i >= l; i--) mat[b][i] = num++; // right to left.
            	b--;
            for(int i = b; i >= t; i--) mat[i][l] = num++; // bottom to top.
            	l++;
        }
        return mat;
    }
}

链接:https://leetcode-cn.com/problems/spiral-matrix-ii/solution/spiral-matrix-ii-mo-ni-fa-she-ding-bian-jie-qing-x/