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