Skip to content

Commit ea83802

Browse files
committed
SPIRAL-MATRIX solution
1 parent 389f3ce commit ea83802

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

spiral-matrix/jdy8739.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number[]}
4+
*/
5+
var spiralOrder = function(matrix) {
6+
let top = 0;
7+
let left = 0;
8+
let bottom = matrix.length - 1;
9+
let right = matrix[0].length - 1;
10+
11+
const answer = [];
12+
13+
while (top <= bottom && left <= right) {
14+
for (let i = left; i <= right; i++) {
15+
answer.push(matrix[top][i]);
16+
}
17+
top++;
18+
19+
if (top > bottom) {
20+
break;
21+
}
22+
23+
for (let j = top; j <= bottom; j++) {
24+
answer.push(matrix[j][right]);
25+
}
26+
right--;
27+
28+
if (left > right) {
29+
break;
30+
}
31+
32+
for (let k = right; k >= left; k--) {
33+
answer.push(matrix[bottom][k]);
34+
}
35+
bottom--;
36+
37+
for (let l = bottom; l >= top; l--) {
38+
answer.push(matrix[l][left]);
39+
}
40+
left++;
41+
}
42+
43+
return answer;
44+
};
45+
46+
// 시간 복잡도 O(m * n)
47+
// 공간 복잡도 O(1)

0 commit comments

Comments
 (0)