给定一个整数n,生成一个由1到n^2的元素填充的螺旋正方形
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
Spiral Matrix II
从左到右,从上到下,从右到左,从下到上依次遍历
同时赋予一个自增的变量值
注意边界的判断
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> result(n, vector<int>(n));
int begin = 0, end = n - 1;
int num = 1;
while (begin < end)
{
for (int j = begin; j < end; ++j)
result[begin][j] = num++;
for (int i = begin; i < end; ++i)
result[i][end] = num++;
for (int j = end; j > begin; --j)
result[end][j] = num++;
for (int i = end; i > begin; --i)
result[i][begin] = num++;
++begin;
--end;
}
if (begin == end)
result[begin][begin] = num++;
return result;
}