-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray-spiral.js
More file actions
64 lines (55 loc) · 1.22 KB
/
Copy patharray-spiral.js
File metadata and controls
64 lines (55 loc) · 1.22 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Spiral Order Matrix
Given a matrix of m * n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example:
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return
[1, 2, 3, 6, 9, 8, 7, 4, 5]
*/
function spiralOrder(A){
const B = [];
const R = A.length;
const C = A[0].length;
let top = 0;
let right = C - 1;
let left = 0;
let bottom = R - 1;
let i = 0;
let dir = 0; // 0 right, 1 down, 2 left, 3 up
while(top <= bottom && left <= right) {
if (dir === 0) {
for (i = left; i <= right; i += 1) {
B.push(A[top][i]);
}
top += 1;
}
else if (dir === 1) {
for (i = top; i <= bottom; i += 1) {
B.push(A[i][right]);
}
right -= 1;
}
else if (dir === 2) {
for (i = right; i >= left; i -= 1) {
B.push(A[bottom][i]);
}
bottom -= 1;
}
else if (dir === 3) {
for (i = bottom; i >= top; i -= 1) {
B.push(A[i][left])
}
left += 1;
}
dir = (dir+1)%4;
}
return B;
}
// Tests
let a = [[1,2,3,4],[5,6,7,8],[9,10,11,12]];
console.log(spiralOrder(a));