Skip to content

Commit 28c8764

Browse files
committed
https://leetcode.cn/problems/zigzag-iterator/
1 parent 92ecb66 commit 28c8764

File tree

22 files changed

+135
-55
lines changed

22 files changed

+135
-55
lines changed

6olJmJ/test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { assertEquals } from "asserts";
2-
32
import explorationSupply from "./index.ts";
43

54
Deno.test("explorationSupply", () => {
65
const station = [2, 7, 8, 10],
76
pos = [4, 9],
8-
输出 = [0, 2];
9-
assertEquals(explorationSupply(station, pos), 输出);
7+
OUTPUT = [0, 2];
8+
assertEquals(explorationSupply(station, pos), OUTPUT);
109
});
1110
Deno.test("explorationSupply", () => {
1211
const station = [2, 5, 8, 14, 17],
1312
pos = [1, 14, 11, 2],
14-
输出 = [0, 3, 2, 0];
15-
assertEquals(explorationSupply(station, pos), 输出);
13+
OUTPUT = [0, 3, 2, 0];
14+
assertEquals(explorationSupply(station, pos), OUTPUT);
1615
});

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,4 +2586,6 @@ https://leetcode.cn/problems/print-in-order/
25862586

25872587
https://leetcode.cn/problems/print-foobar-alternately
25882588

2589+
https://leetcode.cn/problems/zigzag-iterator/
2590+
25892591
</details>

count-anagrams/test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { assertEquals } from "asserts";
2-
32
import countAnagrams from "./index.ts";
43

54
Deno.test("count-anagrams", () => {
65
const s = "too hot",
7-
输出 = 18;
6+
OUTPUT = 18;
87

9-
assertEquals(countAnagrams(s), 输出);
8+
assertEquals(countAnagrams(s), OUTPUT);
109
});
1110
Deno.test("count-anagrams", () => {
1211
const s = "aa",
13-
输出 = 1;
12+
OUTPUT = 1;
1413

15-
assertEquals(countAnagrams(s), 输出);
14+
assertEquals(countAnagrams(s), OUTPUT);
1615
});

minimum-addition-to-make-integer-beautiful/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function makeIntegerBeautiful(
1111
// 记录当前执行到第 base + 1 次
1212
let base = 0;
1313
for (let i = nArr.length - 1; i >= 0; i--) {
14-
// 每次执行判断是否符合结果,若符合直接输出
14+
// 每次执行判断是否符合结果,若符合直接OUTPUT
1515
if (sum <= target) return rz;
1616
const currNum = nArr[i];
1717
// 更新结果
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
import { assertEquals } from "asserts";
2-
32
import makeStringSorted from "./index.ts";
43

54
Deno.test("minimum-number-of-operations-to-make-string-sorted", () => {
65
const s = "leetcodeleetcodeleetcode",
7-
输出 = 982157772;
8-
assertEquals(makeStringSorted(s), 输出);
6+
OUTPUT = 982157772;
7+
assertEquals(makeStringSorted(s), OUTPUT);
98
});
109
Deno.test("minimum-number-of-operations-to-make-string-sorted", () => {
1110
const s = "cdbea",
12-
输出 = 63;
13-
assertEquals(makeStringSorted(s), 输出);
11+
OUTPUT = 63;
12+
assertEquals(makeStringSorted(s), OUTPUT);
1413
});
1514
Deno.test("minimum-number-of-operations-to-make-string-sorted", () => {
1615
const s = "aabaa",
17-
输出 = 2;
18-
assertEquals(makeStringSorted(s), 输出);
16+
OUTPUT = 2;
17+
assertEquals(makeStringSorted(s), OUTPUT);
1918
});
2019
Deno.test("minimum-number-of-operations-to-make-string-sorted", () => {
2120
const s = "cba",
22-
输出 = 5;
23-
assertEquals(makeStringSorted(s), 输出);
21+
OUTPUT = 5;
22+
assertEquals(makeStringSorted(s), OUTPUT);
2423
});

minimum-path-sum/test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { assertStrictEquals } from "https://deno.land/std@0.187.0/testing/asserts.ts";
2-
32
import minPathSum from "./index.ts";
43

54
Deno.test("minimum-path-sum", () => {
@@ -8,14 +7,14 @@ Deno.test("minimum-path-sum", () => {
87
[1, 5, 1],
98
[4, 2, 1],
109
];
11-
const 输出 = 7;
12-
assertStrictEquals(minPathSum(grid), 输出);
10+
const OUTPUT = 7;
11+
assertStrictEquals(minPathSum(grid), OUTPUT);
1312
});
1413
Deno.test("minimum-path-sum", () => {
1514
const grid = [
1615
[1, 2, 3],
1716
[4, 5, 6],
1817
];
19-
const 输出 = 12;
20-
assertStrictEquals(minPathSum(grid), 输出);
18+
const OUTPUT = 12;
19+
assertStrictEquals(minPathSum(grid), OUTPUT);
2120
});
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { assertEquals } from "asserts";
2-
32
import countDifferentSubsequenceGCDs from "./index.ts";
43

54
Deno.test("number-of-different-subsequences-gcds", () => {
65
const nums = [6, 10, 3],
7-
输出 = 5;
8-
assertEquals(countDifferentSubsequenceGCDs(nums), 输出);
6+
OUTPUT = 5;
7+
assertEquals(countDifferentSubsequenceGCDs(nums), OUTPUT);
98
});
109
Deno.test("number-of-different-subsequences-gcds", () => {
1110
const nums = [5, 15, 40, 5, 6],
12-
输出 = 7;
13-
assertEquals(countDifferentSubsequenceGCDs(nums), 输出);
11+
OUTPUT = 7;
12+
assertEquals(countDifferentSubsequenceGCDs(nums), OUTPUT);
1413
});
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import { assertEquals } from "asserts";
2-
32
import canPartitionKSubsets from "./index.ts";
43

54
Deno.test("partition-to-k-equal-sum-subsets", () => {
65
const nums = [4, 3, 2, 3, 5, 2, 1],
76
k = 4,
8-
输出 = true;
7+
OUTPUT = true;
98

10-
assertEquals(canPartitionKSubsets(nums, k), 输出);
9+
assertEquals(canPartitionKSubsets(nums, k), OUTPUT);
1110
});
1211
Deno.test("partition-to-k-equal-sum-subsets", () => {
1312
const nums = [1, 2, 3, 4],
1413
k = 3,
15-
输出 = false;
14+
OUTPUT = false;
1615

17-
assertEquals(canPartitionKSubsets(nums, k), 输出);
16+
assertEquals(canPartitionKSubsets(nums, k), OUTPUT);
1817
});
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { assertEquals } from "asserts";
2-
32
import productQueries from "./index.ts";
43

54
Deno.test("range-product-queries-of-powers", () => {
@@ -9,11 +8,11 @@ Deno.test("range-product-queries-of-powers", () => {
98
[2, 2],
109
[0, 3],
1110
],
12-
输出 = [2, 4, 64];
13-
assertEquals(productQueries(n, queries), 输出);
11+
OUTPUT = [2, 4, 64];
12+
assertEquals(productQueries(n, queries), OUTPUT);
1413
});
1514

1615
Deno.test("range-product-queries-of-powers", () => {
17-
const n = 2, queries = [[0, 0]], 输出 = [2];
18-
assertEquals(productQueries(n, queries), 输出);
16+
const n = 2, queries = [[0, 0]], OUTPUT = [2];
17+
assertEquals(productQueries(n, queries), OUTPUT);
1918
});

redundant-connection-ii/test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { assertEquals } from "asserts";
2-
32
import findRedundantDirectedConnection from "./index.ts";
43

54
Deno.test("redundant-connection-ii", () => {
@@ -8,9 +7,9 @@ Deno.test("redundant-connection-ii", () => {
87
[1, 3],
98
[2, 3],
109
],
11-
输出 = [2, 3];
10+
OUTPUT = [2, 3];
1211

13-
assertEquals(findRedundantDirectedConnection(edges), 输出);
12+
assertEquals(findRedundantDirectedConnection(edges), OUTPUT);
1413
});
1514
Deno.test("redundant-connection-ii", () => {
1615
const edges = [
@@ -20,7 +19,7 @@ Deno.test("redundant-connection-ii", () => {
2019
[4, 1],
2120
[1, 5],
2221
],
23-
输出 = [4, 1];
22+
OUTPUT = [4, 1];
2423

25-
assertEquals(findRedundantDirectedConnection(edges), 输出);
24+
assertEquals(findRedundantDirectedConnection(edges), OUTPUT);
2625
});

shuffle-string/test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { assertEquals } from "asserts";
2-
32
import restoreString from "./index.ts";
43

54
Deno.test("shuffle-string", () => {
6-
const s = "codeleet", indices = [4, 5, 6, 7, 0, 2, 1, 3], 输出 = "leetcode";
7-
assertEquals(restoreString(s, indices), 输出);
5+
const s = "codeleet",
6+
indices = [4, 5, 6, 7, 0, 2, 1, 3],
7+
OUTPUT = "leetcode";
8+
assertEquals(restoreString(s, indices), OUTPUT);
89
});
910

1011
Deno.test("shuffle-string", () => {
11-
const s = "abc", indices = [0, 1, 2], 输出 = "abc";
12-
assertEquals(restoreString(s, indices), 输出);
12+
const s = "abc", indices = [0, 1, 2], OUTPUT = "abc";
13+
assertEquals(restoreString(s, indices), OUTPUT);
1314
});

wMGN0t/test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import { assertEquals } from "asserts";
2-
32
import storedEnergy from "./index.ts";
43

54
Deno.test("storedEnergy", () => {
65
const storeLimit = 10,
76
power = [1, 3, 4, 3, 6],
87
supply = [[0, 2, 3]],
9-
输出 = 4;
10-
assertEquals(storedEnergy(storeLimit, power, supply), 输出);
8+
OUTPUT = 4;
9+
assertEquals(storedEnergy(storeLimit, power, supply), OUTPUT);
1110
});
1211
Deno.test("storedEnergy", () => {
1312
const storeLimit = 6,
1413
power = [6, 5, 2, 1, 0],
1514
supply = [[0, 1, 2], [2, 3, 3]],
16-
输出 = 0;
17-
assertEquals(storedEnergy(storeLimit, power, supply), 输出);
15+
OUTPUT = 0;
16+
assertEquals(storedEnergy(storeLimit, power, supply), OUTPUT);
1817
});

zigzag-iterator/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export default class ZigzagIterator {
2+
#generator: Generator<number, void, unknown>;
3+
#value: number | void;
4+
#done: boolean | undefined;
5+
constructor(v1: number[], v2: number[]) {
6+
this.#generator = ZigzagGenerator(v1, v2);
7+
8+
const result = this.#generator.next();
9+
this.#value = result.value;
10+
this.#done = result.done;
11+
}
12+
13+
next(): number {
14+
const value = this.#value;
15+
const result = this.#generator.next();
16+
this.#value = result.value;
17+
this.#done = result.done;
18+
return value ?? -Infinity;
19+
}
20+
hasNext(): boolean {
21+
return !this.#done;
22+
}
23+
}
24+
25+
function* ZigzagGenerator(
26+
v1: number[],
27+
v2: number[],
28+
): Generator<number, void, unknown> {
29+
if (v1.length < v2.length) {
30+
for (let i = 0; i < v1.length; i++) {
31+
yield v1[i];
32+
yield v2[i];
33+
}
34+
35+
for (let i = v1.length; i < v2.length; i++) {
36+
yield v2[i];
37+
}
38+
} else {
39+
yield* ZigzagGenerator(v2, v1);
40+
}
41+
}

zigzag-iterator/test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import ZigzagIterator from "./index.ts";
2+
import { assertEquals } from "asserts";
3+
4+
Deno.test("zigzag-iterator", () => {
5+
const v1 = [1, 2],
6+
v2 = [3, 4, 5, 6],
7+
OUTPUT = [1, 3, 2, 4, 5, 6];
8+
9+
const zzit = new ZigzagIterator(v1, v2);
10+
11+
const res: number[] = [];
12+
13+
while (zzit.hasNext()) {
14+
res.push(zzit.next());
15+
}
16+
assertEquals(res, OUTPUT);
17+
});
18+
Deno.test("zigzag-iterator", () => {
19+
const v1 = [1],
20+
v2: number[] = [],
21+
OUTPUT = [1];
22+
23+
const zzit = new ZigzagIterator(v1, v2);
24+
25+
const res: number[] = [];
26+
27+
while (zzit.hasNext()) {
28+
res.push(zzit.next());
29+
}
30+
assertEquals(res, OUTPUT);
31+
});
32+
Deno.test("zigzag-iterator", () => {
33+
const v1: number[] = [],
34+
v2 = [1],
35+
OUTPUT = [1];
36+
37+
const zzit = new ZigzagIterator(v1, v2);
38+
39+
const res: number[] = [];
40+
41+
while (zzit.hasNext()) {
42+
res.push(zzit.next());
43+
}
44+
assertEquals(res, OUTPUT);
45+
});

0 commit comments

Comments
 (0)