Leetcode 59 - Spiral matrix II

Note:

  • Update:
    • Just pray it won’t appear in my interview. Not hard, but a pain in the ass.
  • How to initialize an empty 2D array?
    const arr = [...Array(n)].map(e => Array(n))
  • Keep [start, right) style - make the right boundary open so you won’t be lost.
  • Speical case for the last node when n is odd.

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

img

1
2
Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @param {number} n
* @return {number[][]}
*/
var generateMatrix = function (n) {
let matrix = [...Array(n)].map(e => Array(n).fill('.'));
let times = 1;
let start = [0, 0];
let rows = n;
let cols = n;
while (true) {
if ((n*n & 1) === 1 && times === n*n || (n*n & 1) === 0 && times === n*n + 1) break;
for (let j = start[1]; j < cols - 1; j++) {
matrix[start[0]][j] = times++;
start[1]++;
}
for (let i = start[0]; i < rows - 1; i++) {
matrix[i][start[1]] = times++;
start[0]++;;
}
for (let j = start[1]; j > n - cols; j--) {
matrix[start[0]][j] = times++;
start[1]--;;
}
for (let i = start[0]; i > n - rows; i--) {
matrix[i][start[1]] = times++;
start[0]--;;
}
rows--;
cols--;
start[0]++;
start[1]++;
}
if (n*n & 1 === 1) matrix[n>>1][n>>1] = n*n;
return matrix;
};