# 螺旋矩阵 II

<p>给你一个正整数 <code>n</code> ，生成一个包含 <code>1</code> 到 <code>n<sup>2</sup></code> 所有元素，且元素按顺时针顺序螺旋排列的 <code>n x n</code> 正方形矩阵 <code>matrix</code> 。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0059.Spiral%20Matrix%20II/images/spiraln.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入：</strong>n = 3<strong><br />输出：</strong>[[1,2,3],[8,9,4],[7,6,5]]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>n = 1<strong><br />输出：</strong>[[1]]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>1 <= n <= 20</code></li></ul>

## template

```python
class Solution(object):
	def generateMatrix(self, n):
		"""
		:type n: int
		:rtype: List[List[int]]
		"""
		res = [[0] * n for _ in range(n)]
		pos = [0, 0]
		move = (0, 1)
		for index in range(1, n * n + 1):
			res[pos[0]][pos[1]] = index
			if res[(pos[0] + move[0]) % n][(pos[1] + move[1]) % n] > 0:
				move = (move[1], -1 * move[0])
			pos[0] = pos[0] + move[0]
			pos[1] = pos[1] + move[1]
		return res
if __name__ == '__main__':
	s = Solution()
	print (s.generateMatrix(2))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```