File tree Expand file tree Collapse file tree 3 files changed +18
-27
lines changed
solution/1300-1399/1306.Jump Game III Expand file tree Collapse file tree 3 files changed +18
-27
lines changed Original file line number Diff line number Diff line change @@ -180,19 +180,16 @@ func canReach(arr []int, start int) bool {
180
180
181
181
``` ts
182
182
function canReach(arr : number [], start : number ): boolean {
183
- const q: number [] = [start ];
184
- while (q .length ) {
185
- const i: number = q .shift ()! ;
183
+ const q = [start ];
184
+ for (const i of q ) {
186
185
if (arr [i ] === 0 ) {
187
186
return true ;
188
187
}
189
- const x: number = arr [i ];
190
- arr [i ] = - 1 ;
191
- for (const j of [i + x , i - x ]) {
192
- if (j >= 0 && j < arr .length && arr [j ] !== - 1 ) {
193
- q .push (j );
194
- }
188
+ if (arr [i ] === - 1 || arr [i ] === undefined ) {
189
+ continue ;
195
190
}
191
+ q .push (i + arr [i ], i - arr [i ]);
192
+ arr [i ] = - 1 ;
196
193
}
197
194
return false ;
198
195
}
Original file line number Diff line number Diff line change @@ -169,19 +169,16 @@ func canReach(arr []int, start int) bool {
169
169
170
170
``` ts
171
171
function canReach(arr : number [], start : number ): boolean {
172
- const q: number [] = [start ];
173
- while (q .length ) {
174
- const i: number = q .shift ()! ;
172
+ const q = [start ];
173
+ for (const i of q ) {
175
174
if (arr [i ] === 0 ) {
176
175
return true ;
177
176
}
178
- const x: number = arr [i ];
179
- arr [i ] = - 1 ;
180
- for (const j of [i + x , i - x ]) {
181
- if (j >= 0 && j < arr .length && arr [j ] !== - 1 ) {
182
- q .push (j );
183
- }
177
+ if (arr [i ] === - 1 || arr [i ] === undefined ) {
178
+ continue ;
184
179
}
180
+ q .push (i + arr [i ], i - arr [i ]);
181
+ arr [i ] = - 1 ;
185
182
}
186
183
return false ;
187
184
}
Original file line number Diff line number Diff line change 1
1
function canReach ( arr : number [ ] , start : number ) : boolean {
2
- const q : number [ ] = [ start ] ;
3
- while ( q . length ) {
4
- const i : number = q . shift ( ) ! ;
2
+ const q = [ start ] ;
3
+ for ( const i of q ) {
5
4
if ( arr [ i ] === 0 ) {
6
5
return true ;
7
6
}
8
- const x : number = arr [ i ] ;
9
- arr [ i ] = - 1 ;
10
- for ( const j of [ i + x , i - x ] ) {
11
- if ( j >= 0 && j < arr . length && arr [ j ] !== - 1 ) {
12
- q . push ( j ) ;
13
- }
7
+ if ( arr [ i ] === - 1 || arr [ i ] === undefined ) {
8
+ continue ;
14
9
}
10
+ q . push ( i + arr [ i ] , i - arr [ i ] ) ;
11
+ arr [ i ] = - 1 ;
15
12
}
16
13
return false ;
17
14
}
You can’t perform that action at this time.
0 commit comments