description:
螺旋型填充矩阵
Note:
Example:
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
answer:
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> res(n, vector<int>(n, 0));
int up = 0, down = n - 1, right = n - 1, left = 0, val = 1;
while(true) {
for (int j = left; j <= right; ++j) res[up][j] = val++;
if (++up > down) break;
for (int i = up; i <= down; ++i) res[i][right] = val++;
if (--right < left) break;
for (int j = right; j >= left; --j) res[down][j] = val++;
if (--down < up) break;
for (int i = down; i >= up; --i) res[i][left] = val++;
if (++left > right) break;
}
return res;
}
};