|
| 1 | +# 59. Spiral Matrix II - LeetCode Solution |
| 2 | +LeetCode problem link: [59. Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii) |
| 3 | + |
| 4 | +## LeetCode problem description |
| 5 | +Given a positive integer `n`, generate an `n x n` `matrix` filled with elements from `1` to `n * n` in spiral order. |
| 6 | + |
| 7 | +### Example 1 |
| 8 | + |
| 9 | +```ruby |
| 10 | +Input: n = 3 |
| 11 | +Output: [[1,2,3],[8,9,4],[7,6,5]] |
| 12 | +``` |
| 13 | + |
| 14 | +### Example 2 |
| 15 | +```ruby |
| 16 | +Input: n = 1 |
| 17 | +Output: [[1]] |
| 18 | +``` |
| 19 | + |
| 20 | +### Constraints |
| 21 | +- `1 <= n <= 20` |
| 22 | + |
| 23 | +## Intuition behind the Solution |
| 24 | +* The difficulty of this question lies in the control of the two-dimensional array index. |
| 25 | + |
| 26 | +* You only need to use a `get_increment(i, j)` function to specifically control the index of the next two-dimensional array. |
| 27 | + |
| 28 | +## Steps to the Solution |
| 29 | +1. Initialize `increments` and `increment_index`: |
| 30 | + ```python |
| 31 | + increments = [(0, 1), (1, 0), (0, -1), (-1, 0)] # (i, j) right, down, left, up |
| 32 | + increment_index = 0 |
| 33 | + ``` |
| 34 | + |
| 35 | +2. Core logic: |
| 36 | + ```python |
| 37 | + while num <= n * n: |
| 38 | + matrix[i][j] = num |
| 39 | + num += 1 |
| 40 | + |
| 41 | + increment = get_increment(i, j) |
| 42 | + i += increment[0] |
| 43 | + j += increment[1] |
| 44 | + ``` |
| 45 | + |
| 46 | +3. For function `get_increment(i, j)`, it should return a pair like `[0, 1]`. First verify whether the current increment is valid. If not, use the next increment. |
| 47 | + ```python |
| 48 | + def get_increment(i, j): |
| 49 | + increment = increments[increment_index] |
| 50 | + i += increment[0] |
| 51 | + j += increment[1] |
| 52 | + |
| 53 | + if ( |
| 54 | + i < 0 or i >= len(matrix) or |
| 55 | + j < 0 or j >= len(matrix) or |
| 56 | + matrix[i][j] is not None |
| 57 | + ): # not valid, use next increment |
| 58 | + increment_index += 1 |
| 59 | + increment_index %= len(self.increments) |
| 60 | + |
| 61 | + return increments[increment_index] |
| 62 | + ``` |
| 63 | + |
| 64 | +## Complexity |
| 65 | +* Time: `O(n * n)`. |
| 66 | +* Space: `O(n * n)`. |
| 67 | + |
| 68 | +## Java |
| 69 | +```java |
| 70 | +class Solution { |
| 71 | + private int[][] matrix; |
| 72 | + private int[][] increments = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; |
| 73 | + private int incrementIndex = 0; |
| 74 | + |
| 75 | + public int[][] generateMatrix(int n) { |
| 76 | + matrix = new int[n][n]; |
| 77 | + var i = 0; |
| 78 | + var j = 0; |
| 79 | + var num = 1; |
| 80 | + |
| 81 | + while (num <= n * n) { |
| 82 | + matrix[i][j] = num; |
| 83 | + num++; |
| 84 | + |
| 85 | + var increment = getIncrement(i, j); |
| 86 | + i += increment[0]; |
| 87 | + j += increment[1]; |
| 88 | + } |
| 89 | + |
| 90 | + return matrix; |
| 91 | + } |
| 92 | + |
| 93 | + private int[] getIncrement(int i, int j) { |
| 94 | + var increment = increments[incrementIndex]; |
| 95 | + i += increment[0]; |
| 96 | + j += increment[1]; |
| 97 | + |
| 98 | + if ( |
| 99 | + i < 0 || i >= matrix.length || |
| 100 | + j < 0 || j >= matrix.length || |
| 101 | + matrix[i][j] > 0 |
| 102 | + ) { |
| 103 | + incrementIndex += 1; |
| 104 | + incrementIndex %= increments.length; |
| 105 | + } |
| 106 | + |
| 107 | + return increments[incrementIndex]; |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Python |
| 113 | +```python |
| 114 | +class Solution: |
| 115 | + def __init__(self): |
| 116 | + self.matrix = None |
| 117 | + self.increments = [(0, 1), (1, 0), (0, -1), (-1, 0)] |
| 118 | + self.increment_index = 0 |
| 119 | + |
| 120 | + def generateMatrix(self, n: int) -> List[List[int]]: |
| 121 | + self.matrix = [[None] * n for _ in range(n)] |
| 122 | + i = 0 |
| 123 | + j = 0 |
| 124 | + num = 1 |
| 125 | + |
| 126 | + while num <= n * n: |
| 127 | + self.matrix[i][j] = num |
| 128 | + num += 1 |
| 129 | + |
| 130 | + increment = self.get_increment(i, j) |
| 131 | + i += increment[0] |
| 132 | + j += increment[1] |
| 133 | + |
| 134 | + return self.matrix |
| 135 | + |
| 136 | + def get_increment(self, i, j): |
| 137 | + increment = self.increments[self.increment_index] |
| 138 | + i += increment[0] |
| 139 | + j += increment[1] |
| 140 | + |
| 141 | + if ( |
| 142 | + i < 0 or i >= len(self.matrix) or |
| 143 | + j < 0 or j >= len(self.matrix) or |
| 144 | + self.matrix[i][j] |
| 145 | + ): |
| 146 | + self.increment_index += 1 |
| 147 | + self.increment_index %= len(self.increments) |
| 148 | + |
| 149 | + return self.increments[self.increment_index] |
| 150 | +``` |
| 151 | + |
| 152 | +## C++ |
| 153 | +```cpp |
| 154 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 155 | +``` |
| 156 | + |
| 157 | +## JavaScript |
| 158 | +```javascript |
| 159 | +let matrix |
| 160 | +const increments = [[0, 1], [1, 0], [0, -1], [-1, 0]] |
| 161 | +let incrementIndex |
| 162 | + |
| 163 | +var generateMatrix = function (n) { |
| 164 | + matrix = Array(n).fill().map(() => Array(n).fill(0)) |
| 165 | + incrementIndex = 0 |
| 166 | + |
| 167 | + let i = 0 |
| 168 | + let j = 0 |
| 169 | + let num = 1 |
| 170 | + |
| 171 | + while (num <= n * n) { |
| 172 | + matrix[i][j] = num |
| 173 | + num++ |
| 174 | + |
| 175 | + const increment = getIncrement(i, j) |
| 176 | + i += increment[0] |
| 177 | + j += increment[1] |
| 178 | + } |
| 179 | + |
| 180 | + return matrix |
| 181 | +}; |
| 182 | + |
| 183 | +function getIncrement(i, j) { |
| 184 | + const increment = increments[incrementIndex] |
| 185 | + i += increment[0] |
| 186 | + j += increment[1] |
| 187 | + |
| 188 | + if ( |
| 189 | + i < 0 || i >= matrix.length || |
| 190 | + j < 0 || j >= matrix.length || |
| 191 | + matrix[i][j] > 0 |
| 192 | + ) { |
| 193 | + incrementIndex += 1 |
| 194 | + incrementIndex %= increments.length |
| 195 | + } |
| 196 | + |
| 197 | + return increments[incrementIndex] |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +## C# |
| 202 | +```c# |
| 203 | +public class Solution |
| 204 | +{ |
| 205 | + int[][] matrix; |
| 206 | + int[][] increments = { new[] {0, 1}, new[] {1, 0}, new[] {0, -1}, new[] {-1, 0} }; |
| 207 | + int incrementIndex = 0; |
| 208 | + |
| 209 | + public int[][] GenerateMatrix(int n) |
| 210 | + { |
| 211 | + matrix = new int[n][]; |
| 212 | + |
| 213 | + for (int k = 0; k < n; k++) |
| 214 | + matrix[k] = new int[n]; |
| 215 | + |
| 216 | + int i = 0; |
| 217 | + int j = 0; |
| 218 | + int num = 1; |
| 219 | + |
| 220 | + while (num <= n * n) |
| 221 | + { |
| 222 | + matrix[i][j] = num; |
| 223 | + num++; |
| 224 | + |
| 225 | + int[] increment = getIncrement(i, j); |
| 226 | + i += increment[0]; |
| 227 | + j += increment[1]; |
| 228 | + } |
| 229 | + |
| 230 | + return matrix; |
| 231 | + } |
| 232 | + |
| 233 | + int[] getIncrement(int m, int n) |
| 234 | + { |
| 235 | + int[] increment = increments[incrementIndex]; |
| 236 | + int i = m + increment[0]; |
| 237 | + int j = n + increment[1]; |
| 238 | + |
| 239 | + if ( |
| 240 | + i < 0 || i >= matrix.GetLength(0) || |
| 241 | + j < 0 || j >= matrix.GetLength(0) || |
| 242 | + matrix[i][j] > 0 |
| 243 | + ) |
| 244 | + { |
| 245 | + incrementIndex += 1; |
| 246 | + incrementIndex %= increments.Length; |
| 247 | + } |
| 248 | + |
| 249 | + return increments[incrementIndex]; |
| 250 | + } |
| 251 | +} |
| 252 | +``` |
| 253 | + |
| 254 | +## Go |
| 255 | +```go |
| 256 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 257 | +``` |
| 258 | + |
| 259 | +## Ruby |
| 260 | +```ruby |
| 261 | +# Welcome to create a PR to complete the code of this language, thanks! |
| 262 | +``` |
| 263 | + |
| 264 | +## C |
| 265 | +```c |
| 266 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 267 | +``` |
| 268 | + |
| 269 | +## Kotlin |
| 270 | +```kotlin |
| 271 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 272 | +``` |
| 273 | + |
| 274 | +## Swift |
| 275 | +```swift |
| 276 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 277 | +``` |
| 278 | + |
| 279 | +## Rust |
| 280 | +```rust |
| 281 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 282 | +``` |
| 283 | + |
| 284 | +## Other languages |
| 285 | +``` |
| 286 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 287 | +``` |
0 commit comments