Skip to content

Commit 4ca65d6

Browse files
authored
refactor: update ts solution to lc problem: No.1306 (#3018)
1 parent 428e08e commit 4ca65d6

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,16 @@ 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()!;
183+
const q = [start];
184+
for (const i of q) {
186185
if (arr[i] === 0) {
187186
return true;
188187
}
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;
195190
}
191+
q.push(i + arr[i], i - arr[i]);
192+
arr[i] = -1;
196193
}
197194
return false;
198195
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,16 @@ 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()!;
172+
const q = [start];
173+
for (const i of q) {
175174
if (arr[i] === 0) {
176175
return true;
177176
}
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;
184179
}
180+
q.push(i + arr[i], i - arr[i]);
181+
arr[i] = -1;
185182
}
186183
return false;
187184
}
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
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) {
54
if (arr[i] === 0) {
65
return true;
76
}
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;
149
}
10+
q.push(i + arr[i], i - arr[i]);
11+
arr[i] = -1;
1512
}
1613
return false;
1714
}

0 commit comments

Comments
 (0)