# 螺旋矩阵 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

```java
package LeetCode;

public class GenerateMatrix {
	public int[][] generateMatrix(int n) {
		int[][] res = new int[n][n];
		if (n == 0) {
			return res;
		}
		int left = 0;
		int right = n - 1;
		int up = 0;
		int down = n - 1;
		int i = 1;
		while (i <= n * n) {

			for (int col = left; col <= right; col++) {
				res[up][col] = i;
				i++;
			}

			up++;
			if (i <= n * n) {
				for (int j = up; j <= down; j++) {
					res[j][right] = i;
					i++;
				}
				right--;
			}
			if (i <= n * n) {
				for (int j = right; j >= left; j--) {
					res[down][j] = i;
					i++;
				}
				down--;
			}
			if (i <= n * n) {
				for (int j = down; j >= up; j--) {
					res[j][left] = i;
					i++;
				}
				left++;
			}
		}
		return res;
	}

	public static void main(String[] args) {
		GenerateMatrix a = new GenerateMatrix();
		a.generateMatrix(3);
	}
}
```

## 答案

```java

```

## 选项

### A

```java

```

### B

```java

```

### C

```java

```