class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
if (n == 0) return vector<vector<int>>();
vector<vector<int>> arr(n);
for (vector<int>& R : arr) R = move(vector<int>(n));
int num = 0;
int fr = 0, lr, fc = 0, lc; //first_row=0, first_column=0
for (; n > 1; n -= 2) {
lr = fr + n - 1; //last_row = first_row + n - 1
lc = fc + n - 1; //last_column = first_column + n - 1
for (int c = fc; c < lc; ++c) //first row: fc ~ lc-1
arr[fr][c] = ++num;
for (int r = fr; r < lr; ++r) //last column: fr ~ lr-1
arr[r][lc] = ++num;
for (int c = lc; c > fc; --c) //last row: lc ~ fc+1
arr[lr][c] = ++num;
for (int r = lr; r > fr; --r) //first column: lr ~ fr+1
arr[r][fc] = ++num;
++fr;
++fc;
}
n ? arr[fr][fc] = ++num : 0;
return arr;
}
};