-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path59.js
51 lines (43 loc) · 882 Bytes
/
59.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @param {number} n
* @return {number[][]}
*/
const generateMatrix = (n) => {
const matrix = [];
for (let i = 0; i < n; i += 1) {
matrix[i] = [];
}
const directions = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
const dirs = directions.length;
const limits = [
[0, n - 1],
[n - 1, n - 1],
[n - 1, 0],
[1, 0],
];
const changes = [
[1, -1],
[-1, -1],
[-1, 1],
[1, 1],
];
let curDir = 0;
let curRow = 0;
let curCol = 0;
for (let i = 1; i <= n * n; i += 1) {
matrix[curRow][curCol] = i;
if (curRow === limits[curDir][0] && curCol === limits[curDir][1]) {
limits[curDir][0] += changes[curDir][0];
limits[curDir][1] += changes[curDir][1];
curDir = (curDir + 1) % dirs;
}
curRow += directions[curDir][0];
curCol += directions[curDir][1];
}
return matrix;
};