File tree Expand file tree Collapse file tree 1 file changed +11
-16
lines changed
solution/0800-0899/0885.Spiral Matrix III Expand file tree Collapse file tree 1 file changed +11
-16
lines changed Original file line number Diff line number Diff line change 6
6
* @return {number[][] }
7
7
*/
8
8
var spiralMatrixIII = function ( rows , cols , rStart , cStart ) {
9
- let result = [ ] ;
10
- let totalCells = rows * cols ;
11
- let directions = [
9
+ const ans = [ ] ;
10
+ const totalCells = rows * cols ;
11
+ const directions = [
12
12
[ 0 , 1 ] ,
13
13
[ 1 , 0 ] ,
14
14
[ 0 , - 1 ] ,
15
15
[ - 1 , 0 ] ,
16
16
] ;
17
17
let step = 0 ;
18
18
let d = 0 ;
19
- let r = rStart ,
20
- c = cStart ;
21
-
22
- result . push ( [ r , c ] ) ;
23
-
24
- while ( result . length < totalCells ) {
25
- if ( d === 0 || d === 2 ) step ++ ;
26
-
19
+ let [ r , c ] = [ rStart , cStart ] ;
20
+ ans . push ( [ r , c ] ) ;
21
+ while ( ans . length < totalCells ) {
22
+ if ( d === 0 || d === 2 ) {
23
+ step ++ ;
24
+ }
27
25
for ( let i = 0 ; i < step ; i ++ ) {
28
26
r += directions [ d ] [ 0 ] ;
29
27
c += directions [ d ] [ 1 ] ;
30
-
31
28
if ( r >= 0 && r < rows && c >= 0 && c < cols ) {
32
- result . push ( [ r , c ] ) ;
29
+ ans . push ( [ r , c ] ) ;
33
30
}
34
31
}
35
-
36
32
d = ( d + 1 ) % 4 ;
37
33
}
38
-
39
- return result ;
34
+ return ans ;
40
35
} ;
You can’t perform that action at this time.
0 commit comments