@@ -24,47 +24,45 @@ Input:
24
24
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
25
25
*/
26
26
27
-
28
27
/**
29
28
* @param {number[][] } matrix
30
29
* @return {number[] }
31
30
*/
32
31
33
- var spiralOrder = function ( matrix ) {
34
- if ( matrix . length === 0 )
35
- return [ ] ;
36
-
37
- var retArray = [ ] ;
38
- const rowLength = matrix . length ;
39
- const colLength = matrix [ 0 ] . length ;
40
- const countRectangles = Math . ceil ( Math . min ( colLength , rowLength ) / 2 )
41
- for ( var i = 0 ; i < countRectangles ; i ++ )
42
- printRect ( matrix , i , rowLength , colLength , retArray ) ;
32
+ var spiralOrder = function ( matrix ) {
33
+ if ( matrix . length === 0 ) return [ ] ;
34
+
35
+ var retArray = [ ] ;
36
+ const rowLength = matrix . length ;
37
+ const colLength = matrix [ 0 ] . length ;
38
+ const countRectangles = Math . ceil ( Math . min ( colLength , rowLength ) / 2 ) ;
39
+ for ( var i = 0 ; i < countRectangles ; i ++ )
40
+ printRect ( matrix , i , rowLength , colLength , retArray ) ;
43
41
44
- return retArray ;
42
+ return retArray ;
45
43
} ;
46
44
47
- var printRect = function ( matrix , i , rowLength , colLength , retArray ) {
45
+ var printRect = function ( matrix , i , rowLength , colLength , retArray ) {
48
46
const firstRow = i ;
49
47
const firstCol = i ;
50
48
const lastRow = rowLength - i - 1 ;
51
49
const lastCol = colLength - i - 1 ;
52
-
53
- for ( var col = firstCol ; col <= lastCol ; col ++ ) {
50
+
51
+ for ( var col = firstCol ; col <= lastCol ; col ++ ) {
54
52
retArray . push ( matrix [ firstRow ] [ col ] ) ;
55
53
}
56
- for ( var row = firstRow + 1 ; row <= lastRow ; row ++ ) {
54
+ for ( var row = firstRow + 1 ; row <= lastRow ; row ++ ) {
57
55
retArray . push ( matrix [ row ] [ lastCol ] ) ;
58
56
}
59
- if ( firstRow === lastRow || firstCol === lastCol ) {
57
+ if ( firstRow === lastRow || firstCol === lastCol ) {
60
58
return ;
61
59
}
62
- for ( var col = lastCol - 1 ; col >= firstCol ; col -- ) {
60
+ for ( var col = lastCol - 1 ; col >= firstCol ; col -- ) {
63
61
retArray . push ( matrix [ lastRow ] [ col ] ) ;
64
62
}
65
- for ( var row = lastRow - 1 ; row > firstRow ; row -- ) {
63
+ for ( var row = lastRow - 1 ; row > firstRow ; row -- ) {
66
64
retArray . push ( matrix [ row ] [ firstCol ] ) ;
67
65
}
68
- }
66
+ } ;
69
67
70
68
module . exports . spiralOrder = spiralOrder ;
0 commit comments