File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[][] } matrix
3+ * @return {number[] }
4+ */
5+ var spiralOrder = function ( matrix ) {
6+ if ( matrix . length === 0 ) return [ ] ;
7+
8+ const res = [ ] ;
9+ let left = 0 , right = matrix [ 0 ] . length ;
10+ let top = 0 , bottom = matrix . length ;
11+
12+ while ( left < right && top < bottom ) {
13+ // 상단 행 왼쪽 → 오른쪽
14+ for ( let i = left ; i < right ; i ++ ) {
15+ res . push ( matrix [ top ] [ i ] ) ;
16+ }
17+ top += 1 ;
18+
19+ // 오른쪽 열 위 → 아래
20+ for ( let i = top ; i < bottom ; i ++ ) {
21+ res . push ( matrix [ i ] [ right - 1 ] ) ;
22+ }
23+ right -= 1 ;
24+
25+ if ( ! ( left < right && top < bottom ) ) break ;
26+
27+ // 하단 행 오른쪽 → 왼쪽
28+ for ( let i = right - 1 ; i >= left ; i -- ) {
29+ res . push ( matrix [ bottom - 1 ] [ i ] ) ;
30+ }
31+ bottom -= 1 ;
32+
33+ // 왼쪽 열 아래 → 위
34+ for ( let i = bottom - 1 ; i >= top ; i -- ) {
35+ res . push ( matrix [ i ] [ left ] ) ;
36+ }
37+ left += 1 ;
38+ }
39+
40+ return res ;
41+ } ;
You can’t perform that action at this time.
0 commit comments