Skip to content

Commit 9dca46e

Browse files
authored
feat: add ts solution to lc problem: No.2757 (doocs#1554)
1 parent e24f19f commit 9dca46e

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

solution/2700-2799/2757.Generate Circular Array Values/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@
8080

8181
```ts
8282
function* cycleGenerator(arr: number[], startIndex: number): Generator<number, void, number> {
83-
let i = startIndex;
84-
let x = yield arr[i];
83+
const n = arr.length;
8584
while (true) {
86-
i = (i + x + 10000 * arr.length) % arr.length;
87-
x = yield arr[i];
85+
const jump = yield arr[startIndex];
86+
startIndex = (((startIndex + jump) % n) + n) % n;
8887
}
8988
}
9089
/**

solution/2700-2799/2757.Generate Circular Array Values/README_EN.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@
7272

7373
```ts
7474
function* cycleGenerator(arr: number[], startIndex: number): Generator<number, void, number> {
75-
let i = startIndex;
76-
let x = yield arr[i];
75+
const n = arr.length;
7776
while (true) {
78-
i = (i + x + 10000 * arr.length) % arr.length;
79-
x = yield arr[i];
77+
const jump = yield arr[startIndex];
78+
startIndex = (((startIndex + jump) % n) + n) % n;
8079
}
8180
}
8281
/**

solution/2700-2799/2757.Generate Circular Array Values/Solution.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
function* cycleGenerator(arr: number[], startIndex: number): Generator<number, void, number> {
2-
let i = startIndex;
3-
let x = yield arr[i];
2+
const n = arr.length;
43
while (true) {
5-
i = (i + x + 10000 * arr.length) % arr.length;
6-
x = yield arr[i];
4+
const jump = yield arr[startIndex];
5+
startIndex = (((startIndex + jump) % n) + n) % n;
76
}
87
}
8+
99
/**
1010
* const gen = cycleGenerator([1,2,3,4,5], 0);
1111
* gen.next().value // 1

0 commit comments

Comments
 (0)