forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiral Matrix II.js
39 lines (39 loc) · 1.06 KB
/
Spiral Matrix II.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
// Runtime: 51 ms (Top 99.51%) | Memory: 42.1 MB (Top 65.95%)
var generateMatrix = function(n) {
const arr = new Array(n).fill(0).map(() => new Array(n).fill(0));
let count = 1, index = 1, i = 0, j =0, changed = false, toIncrease = true;
arr[i][j] = index;
while(index < n*n) {
index++;
if(i == n-count && j > count-1) {
j--;
toIncrease = false;
changed = true;
}
else if(i !== n-count && j == n-count) {
i++;
toIncrease = false;
changed = true;
}
if(i == count-1 && !changed) {
if(toIncrease) j++;
else j--;
}
else if(j == count-1 && !changed) {
if(i == count) {
toIncrease = true;
j++;
count++;
}
else if(toIncrease) i++;
else i--;
}
arr[i][j] = index;
if(index == 4*(n-1)) {
toIncrease = true;
count++;
}
changed = false;
}
return arr;
};