File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-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
+ 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)
You can’t perform that action at this time.
0 commit comments