class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
if(n == 0) return {};
vector<vector<int>> v(n,vector<int>(n,0));
int up = 0,right = n - 1,down = n - 1,left = 0;
int size = n * n,i = 0;
while(i != size){
for(int x = left,y = up; x <= right; x++) v[y][x] = ++i;up++;
if(i == size) break;
for(int x = right,y = up;y <= down; y++) v[y][x] = ++i;right--;
if(i == size) break;
for(int x = right,y = down;x>=left;x--) v[y][x] = ++i;down--;
if(i == size) break;
for(int x = left,y = down;y>=up;y--) v[y][x] = ++i;left++;
if(i == size) break;
}
return v;
}
};
08-25