Note:
跟54螺旋矩阵几乎是一模一样了
代码如下:
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
vector<vector<int>> res(n, vector<int>(n));
for(int i = 1, x = 0, y = 0, d = 0; i <= n * n; i ++){
res[x][y] = i;
int a = x + dx[d], b = y + dy[d];
if(a < 0 || a >= n || b < 0 || b >= n || res[a][b]){
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
}
return res;
}
};