算法精粹(algorithm-essentials)

感谢soulmachine@github提供内容
## Spiral Matrix II


### 描述

Given an integer `n`, generate a square matrix filled with elements from 1 to `n^2` in spiral order.

For example,
Given `n = 3`,

You should return the following matrix:

```
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
```


### 分析

这题比上一题要简单。


### 代码1

{% codesnippet "./code/spiral-matrix-ii-1."+book.suffix, language=book.suffix %}{% endcodesnippet %}


### 代码2

```cpp
// LeetCode, Spiral Matrix II
// @author 龚陆安 (http://weibo.com/luangong)
// 时间复杂度O(n^2),空间复杂度O(n^2)
class Solution {
public:
vector > generateMatrix(int n) {
vector< vector > matrix(n, vector(n));
if (n == 0) return matrix;
int beginX = 0, endX = n - 1;
int beginY = 0, endY = n - 1;
int num = 1;
while (true) {
for (int j = beginX; j <= endX; ++j) matrix[beginY][j] = num++;
if (++beginY > endY) break;

for (int i = beginY; i <= endY; ++i) matrix[i][endX] = num++;
if (beginX > --endX) break;

for (int j = endX; j >= beginX; --j) matrix[endY][j] = num++;
if (beginY > --endY) break;

for (int i = endY; i >= beginY; --i) matrix[i][beginX] = num++;
if (++beginX > endX) break;
}
return matrix;
}
};
```


### 相关题目

* [Spiral Matrix](spiral-matrix.md)