-
Notifications
You must be signed in to change notification settings - Fork 0
/
54.Spiral Matrix.js
63 lines (57 loc) · 1.22 KB
/
54.Spiral Matrix.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
52
53
54
55
56
57
58
59
60
61
62
63
var spiralOrder = function (mat) {
if (mat.length == 0 || mat[0].length == 0) {
return mat;
}
var ans = [];
var direction = "right";
var top = 0;
var left = 0;
var right = mat[0].length - 1;
var bottom = mat.length - 1;
while (top <= bottom && left <= right) {
if (direction == "right") {
for (var i = left; i <= right; i++) {
ans.push(mat[top][i]);
}
top++;
direction = "down";
} else if (direction == "down") {
for (var i = top; i <= bottom; i++) {
ans.push(mat[i][right]);
}
right--;
direction = "left";
} else if (direction == "left") {
for (var i = right; i >= left; i--) {
ans.push(mat[bottom][i]);
}
bottom--;
direction = "up";
} else if (direction == "up") {
for (var i = bottom; i >= top; i--) {
ans.push(mat[i][left]);
}
left++;
direction = "right";
}
}
return ans;
};
console.log(
spiralOrder([
[1, 2, 3, 11],
[4, 5, 6, 13],
[7, 8, 9, 15],
[16, 17, 18, 19],
[16, 17, 18, 19],
])
);
console.log(
spiralOrder([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])
);
console.log(spiralOrder([]));
console.log(spiralOrder([[1, 2, 3]]));