|
| 1 | +### [59\. Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) |
| 2 | + |
| 3 | +Difficulty: **Medium** |
| 4 | + |
| 5 | + |
| 6 | +Given a positive integer _n_, generate a square matrix filled with elements from 1 to _n_<sup>2</sup> in spiral order. |
| 7 | + |
| 8 | +**Example:** |
| 9 | + |
| 10 | +``` |
| 11 | +Input: 3 |
| 12 | +Output: |
| 13 | +[ |
| 14 | + [ 1, 2, 3 ], |
| 15 | + [ 8, 9, 4 ], |
| 16 | + [ 7, 6, 5 ] |
| 17 | +] |
| 18 | +``` |
| 19 | + |
| 20 | + |
| 21 | +#### MySolution |
| 22 | + |
| 23 | +Language: **Java** |
| 24 | + |
| 25 | +```java |
| 26 | +class Solution { |
| 27 | + public int[][] generateMatrix(int n) { |
| 28 | + if (n == 0) { |
| 29 | + return new int[0][0]; |
| 30 | + } |
| 31 | + int[][] matrix = new int[n][n]; |
| 32 | + int x = 0; // 需要填充的坐标 |
| 33 | + int y = 0; // 需要填充的坐标 |
| 34 | + int direction = 0; // 0-右 1-下 2-左 3-上 |
| 35 | + for (int i = 1; i <= n * n; i++) { |
| 36 | + matrix[x][y] = i; // 写 |
| 37 | + // 判断下一个位置走向,索引定位到下一个位置 |
| 38 | + switch (direction) { |
| 39 | + case 0: |
| 40 | + if (available(matrix, n, x, y + 1)) { |
| 41 | + y++; |
| 42 | + } else { |
| 43 | + direction = 1; |
| 44 | + x++; |
| 45 | + } |
| 46 | + break; |
| 47 | + case 1: |
| 48 | + if (available(matrix, n, x + 1, y)) { |
| 49 | + x++; |
| 50 | + } else { |
| 51 | + direction = 2; |
| 52 | + y--; |
| 53 | + } |
| 54 | + break; |
| 55 | + case 2: |
| 56 | + if (available(matrix, n, x, y - 1)) { |
| 57 | + y--; |
| 58 | + } else { |
| 59 | + direction = 3; |
| 60 | + x--; |
| 61 | + } |
| 62 | + break; |
| 63 | + case 3: |
| 64 | + if (available(matrix, n, x - 1, y)) { |
| 65 | + x--; |
| 66 | + } else { |
| 67 | + direction = 0; |
| 68 | + y++; |
| 69 | + } |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + return matrix; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * 判断坐标是否可用 |
| 78 | + * |
| 79 | + * @param n 大小 |
| 80 | + * @param x 横坐标 |
| 81 | + * @param y 纵坐标 |
| 82 | + * @return 是否可用 |
| 83 | + */ |
| 84 | + private boolean available(int[][] matrix, int n, int x, int y) { |
| 85 | + return !(x >= n || y >= n || x < 0 || y < 0 || matrix[x][y] > 0); |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +#### Recommend Solution |
| 91 | + |
| 92 | +Language: **Java** |
| 93 | + |
| 94 | +```java |
| 95 | +class Solution { |
| 96 | + public static int[][] generateMatrix(int n) { |
| 97 | + int[][] ret = new int[n][n]; |
| 98 | + int left = 0, top = 0; |
| 99 | + int right = n - 1, down = n - 1; |
| 100 | + int count = 1; |
| 101 | + while (left <= right) { |
| 102 | + for (int j = left; j <= right; j++) { |
| 103 | + ret[top][j] = count++; |
| 104 | + } |
| 105 | + top++; |
| 106 | + for (int i = top; i <= down; i++) { |
| 107 | + ret[i][right] = count++; |
| 108 | + } |
| 109 | + right--; |
| 110 | + for (int j = right; j >= left; j--) { |
| 111 | + ret[down][j] = count++; |
| 112 | + } |
| 113 | + down--; |
| 114 | + for (int i = down; i >= top; i--) { |
| 115 | + ret[i][left] = count++; |
| 116 | + } |
| 117 | + left++; |
| 118 | + } |
| 119 | + return ret; |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
0 commit comments