forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0054-spiral-matrix.ts
39 lines (33 loc) · 975 Bytes
/
0054-spiral-matrix.ts
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
function spiralOrder(matrix: number[][]): number[] {
const res: number[] = [];
let left = 0;
let right = matrix[0].length;
let top = 0;
let bottom = matrix.length;
while (left < right && top < bottom) {
// get every i in the top row
for (let i = left; i < right; i++) {
res.push(matrix[top][i]);
}
top += 1;
// get every i in the right column
for (let i = top; i < bottom; i++) {
res.push(matrix[i][right - 1]);
}
right -= 1;
if (!(left < right && top < bottom)) {
break;
}
// get ever i in the bottom row
for (let i = right - 1; i > left - 1; i--) {
res.push(matrix[bottom - 1][i]);
}
bottom -= 1;
// get evet i in the left column
for (let i = bottom - 1; i > top - 1; i--) {
res.push(matrix[i][left]);
}
left += 1;
}
return res;
}