-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path54_spiral_matrix.js
39 lines (33 loc) · 1009 Bytes
/
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
var spiralOrder = function (matrix) {
const result = [];
let top = 0,
bottom = matrix.length - 1;
let left = 0,
right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
// Traverse top row from left to right
for (let i = left; i <= right; i++) {
result.push(matrix[top][i]);
}
top++;
// Traverse right column from top to bottom
for (let i = top; i <= bottom; i++) {
result.push(matrix[i][right]);
}
right--;
// Check if top has crossed bottom or left has crossed right
if (top <= bottom && left <= right) {
// Traverse bottom row from right to left
for (let i = right; i >= left; i--) {
result.push(matrix[bottom][i]);
}
bottom--;
// Traverse left column from bottom to top
for (let i = bottom; i >= top; i--) {
result.push(matrix[i][left]);
}
left++;
}
}
return result;
};