spiral-matrix-ii
夏侯涵映
2023-12-01
public class Solution {
public int[][] generateMatrix(int n) {
int res[][] =new int [n][n];
if(n==0)
{
return res;
}
int x=0,y=0,N=n-1,a=1;
while(x<=N&&y<=N)
{
for(int i=y;i<=N;i++)
{
res[x][i]=a++;
}
for(int i=x+1;i<=N;i++)
{
res[i][N]=a++;
}
for(int i=N-1;i>=y;i--)
{
res[N][i]=a++;
}
for(int i=N-1;i>x;i--)
{
res[i][y]=a++;
}
x++;y++;N--;
}
return res;
}
}