-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.js
29 lines (29 loc) · 882 Bytes
/
Solution.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
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (matrix) {
let m = matrix.length;
if (m === 0) return [];
let res = [];
let top = 0,
bottom = m - 1,
left = 0,
right = matrix[0].length - 1;
while (left < right && bottom > top) {
for (let i = left; i < right; i++) res.push(matrix[top][i]);
for (let i = top; i < bottom; i++) res.push(matrix[i][right]);
for (let i = right; i > left; i--) res.push(matrix[bottom][i]);
for (let i = bottom; i > top; i--) res.push(matrix[i][left]);
top++;
bottom--;
left++;
right--;
}
if (left === right) {
for (i = top; i <= bottom; i++) res.push(matrix[i][left]);
} else if (top === bottom) {
for (i = left; i <= right; i++) res.push(matrix[top][i]);
}
return res;
};