class Solution {
public:
std::vector<std::vector<int>> generateMatrix(int n) {
std::vector<std::vector<int>> res(n, std::vector<int>(n, 0));
int cnt = 1, offset = 1, i,j, loop =n/2, startx = 0, starty = 0; // 每次圈起始位置(startx, starty)
while (loop--){
i = startx, j = starty;
for (;j < n-offset;j++) res[startx][j] = cnt++;// 行不变列变
for (;i < n-offset;i++) res[i][j] = cnt++;// 列不变行变
for (;j > starty;j--) res[i][j] = cnt++;
for (;i > startx;i--) res[i][j] =cnt++;
startx++, starty++, offset++;
}
if (n%2) res[n/2][n/2] = n*n;
return res;
}
};