Skip to content

Commit 2e2caa4

Browse files
committed
refactor: update TS solution to lc problem: No.1306
1 parent 2d20e9d commit 2e2caa4

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

solution/1300-1399/1306.Jump Game III/README.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,18 @@ func canReach(arr []int, start int) bool {
180180

181181
```ts
182182
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+
190192
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-
}
196193
}
194+
197195
return false;
198196
}
199197
```

solution/1300-1399/1306.Jump Game III/README_EN.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,18 @@ func canReach(arr []int, start int) bool {
169169

170170
```ts
171171
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+
179181
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-
}
185182
}
183+
186184
return false;
187185
}
188186
```
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
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+
911
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-
}
1512
}
13+
1614
return false;
1715
}

0 commit comments

Comments
 (0)