class Solution {
public int[][] generateMatrix(int n) {
int x1 = 0,x2 = n-1, y1 = 0,y2 = n-1;
int k = 1;
int [][]res = new int[n][n];
while(x1<=x2&&y1<=y2){
for(int i=x1;i<=x2;i++) res[y1][i]=k++;
for(int i=y1+1;i<=y2;i++) res[i][x2]=k++;
for(int i=x2-1;i>=x1;i--) res[y2][i]=k++;
for(int i=y2-1;i>=y1+1;i--) res[i][x1]=k++;
x1++;
x2--;
y1++;
y2--;
}
return res;
}
}