```
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
int row = 0;
int col = 0;
int number = 1;
vector<vector<int>> res(n, vector<int>(n, 1));
for (int i = n - 1; i > 0; i--) {
while (col <= i) {
res[row][col++] = number++;
}
col--;
while(++row <= i) {
res[row][col] = number++;
}
row--;
while (--col >= n - i - 1) {
res[row][col] = number++;
}
col++;
while (--row > n - i - 1) {
res[row][col] = number++;
}
col++;
row++;
}
return res;
}
};
```