题目链接:点击这里
AC代码:
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> a(n, vector<int>(n));
int x = 0, y = 0;
a[x][y] = 1;
int tot = 1;
while(tot < n * n)
{
while(y + 1 < n && !a[x][y + 1]) a[x][++y] = ++tot;
while(x + 1 < n && !a[x + 1][y]) a[++x][y] = ++tot;
while(y - 1 >= 0 && !a[x][y - 1]) a[x][--y] = ++tot;
while(x - 1 >= 0 && !a[x - 1][y]) a[--x][y] = ++tot;
}
return a;
}
};