1
/**
2
* @param {number[][]} matrix
3
* @return {number[]}
4
*/
5
var spiralOrder = function (matrix) {
6
let [top, bot, left, right, ans] = [
7
0,
8
matrix.length - 1,
9
0,
10
matrix[0].length - 1,
11
[],
12
];
13

14
while (top <= bot && left <= right) {
15
for (let j = left; j <= right; j++) {
16
ans.push(matrix[top][j]);
17
}
18
top++;
19
for (let i = top; i <= bot; i++) {
20
ans.push(matrix[i][right]);
21
}
22
right--;
23
if (bot < top || right < left) {
24
break;
25
}
26
for (let j = right; left <= j; j--) {
27
ans.push(matrix[bot][j]);
28
}
29
bot--;
30
for (let i = bot; top <= i; i--) {
31
ans.push(matrix[i][left]);
32
}
33
left++;
34
}
35
return ans;
36
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0