Skip to content

Commit d1abdc6

Browse files
committed
https://leetcode.cn/problems/add-two-polynomials-represented-as-linked-lists/
1 parent 7458c5b commit d1abdc6

File tree

9 files changed

+183
-56
lines changed

9 files changed

+183
-56
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/add-two-polynomials-represented-as-linked-lists/
14+
1315
https://leetcode.cn/problems/print-immutable-linked-list-in-reverse/
1416

1517
https://leetcode.cn/problems/plus-one-linked-list/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { PolyNode } from "./PolyNode.ts";
2+
export function ArrayToPolyNode(
3+
array: Array<[number, number]>,
4+
): PolyNode | null {
5+
if (array.length === 0) {
6+
return null;
7+
}
8+
const [c, p] = array[0];
9+
const list = new PolyNode(c, p);
10+
let cur = list;
11+
for (let i = 1; i < array.length; i++) {
12+
const v = array[i];
13+
const [c, p] = v;
14+
const l = new PolyNode(c, p);
15+
cur.next = l;
16+
cur = cur.next;
17+
}
18+
19+
return list;
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class PolyNode {
2+
constructor(
3+
public coefficient: number = 0,
4+
public power: number = 0,
5+
public next: PolyNode | null = null,
6+
) {}
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { PolyNode } from "./PolyNode.ts";
2+
3+
export function PolyNodeToArray(
4+
list: PolyNode | null,
5+
): Array<[number, number]> {
6+
if (list === null) {
7+
return [];
8+
}
9+
const array: Array<[number, number]> = [];
10+
let temp: PolyNode | null = list;
11+
while (temp) {
12+
const { coefficient, power } = temp;
13+
array.push([coefficient, power]);
14+
temp = temp.next;
15+
}
16+
return array;
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { PolyNode } from "./PolyNode.ts";
2+
3+
export default function addPoly(
4+
poly1: PolyNode | null,
5+
poly2: PolyNode | null,
6+
): PolyNode | null {
7+
const dummy = new PolyNode();
8+
let node = dummy;
9+
while (poly1 != null && poly2 != null) {
10+
if (poly1.power === poly2.power) {
11+
const nextCoefficient = poly1.coefficient + poly2.coefficient;
12+
const nextPower = poly1.power;
13+
if (nextCoefficient != 0) {
14+
const nextNode = new PolyNode(nextCoefficient, nextPower);
15+
node.next = nextNode;
16+
node = node.next;
17+
}
18+
poly1 = poly1.next;
19+
poly2 = poly2.next;
20+
} else if (poly1.power > poly2.power) {
21+
node.next = new PolyNode(poly1.coefficient, poly1.power);
22+
23+
node = node.next;
24+
poly1 = poly1.next;
25+
} else {
26+
node.next = new PolyNode(poly2.coefficient, poly2.power);
27+
28+
node = node.next;
29+
poly2 = poly2.next;
30+
}
31+
}
32+
if (poly1 != null) node.next = poly1;
33+
else if (poly2 != null) node.next = poly2;
34+
return dummy.next;
35+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import addPoly from "./index.ts";
3+
import { ArrayToPolyNode } from "./ArrayToPolyNode.ts";
4+
import { PolyNodeToArray } from "./PolyNodeToArray.ts";
5+
Deno.test("add-two-polynomials-represented-as-linked-lists", () => {
6+
const cases = [
7+
[[[1, 2]], [[-1, 2]], []],
8+
[
9+
[
10+
[2, 2],
11+
[4, 1],
12+
[3, 0],
13+
],
14+
15+
[
16+
[3, 2],
17+
[-4, 1],
18+
[-1, 0],
19+
],
20+
[
21+
[5, 2],
22+
[2, 0],
23+
],
24+
],
25+
[
26+
[[1, 1]],
27+
[[1, 0]],
28+
[
29+
[1, 1],
30+
[1, 0],
31+
],
32+
],
33+
];
34+
35+
cases.forEach(([a, b, c]) => {
36+
assertEquals(
37+
c,
38+
PolyNodeToArray(
39+
addPoly(
40+
ArrayToPolyNode(a as [number, number][]),
41+
ArrayToPolyNode(b as [number, number][])
42+
)
43+
)
44+
);
45+
});
46+
});
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
export class ImmutableListNode {
2-
#print;
3-
#index = 0;
4-
#array: number[];
5-
constructor(array: Array<number>, print: (value: number) => void) {
6-
if (array.length === 0) throw Error("input empty array");
7-
this.#print = print;
8-
this.#array = array;
9-
}
10-
printValue() {
11-
this.#print(this.#array[this.#index]);
12-
}
13-
getNext(): ImmutableListNode | null {
14-
if (this.#index+1 >= this.#array.length) return null;
15-
const next = new ImmutableListNode(this.#array, this.#print);
16-
next.#index = this.#index + 1;
17-
return next;
18-
}
19-
}
1+
export class ImmutableListNode {
2+
#print;
3+
#index = 0;
4+
#array: number[];
5+
constructor(array: Array<number>, print: (value: number) => void) {
6+
if (array.length === 0) throw Error("input empty array");
7+
this.#print = print;
8+
this.#array = array;
9+
}
10+
printValue() {
11+
this.#print(this.#array[this.#index]);
12+
}
13+
getNext(): ImmutableListNode | null {
14+
if (this.#index + 1 >= this.#array.length) return null;
15+
const next = new ImmutableListNode(this.#array, this.#print);
16+
next.#index = this.#index + 1;
17+
return next;
18+
}
19+
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { ImmutableListNode } from "./ImmutableListNode.ts";
2-
3-
export default function printLinkedListInReverse(
4-
head: ImmutableListNode | null
5-
) {
6-
if (head !== null) {
7-
printLinkedListInReverse(head.getNext());
8-
head.printValue();
9-
}
10-
}
1+
import { ImmutableListNode } from "./ImmutableListNode.ts";
2+
3+
export default function printLinkedListInReverse(
4+
head: ImmutableListNode | null,
5+
) {
6+
if (head !== null) {
7+
printLinkedListInReverse(head.getNext());
8+
head.printValue();
9+
}
10+
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import { ImmutableListNode } from "./ImmutableListNode.ts";
2-
import printLinkedListInReverse from "./index.ts";
3-
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
4-
5-
Deno.test("print-immutable-linked-list-in-reverse", () => {
6-
const cases = [
7-
[
8-
[1, 2, 3, 4],
9-
[4, 3, 2, 1],
10-
],
11-
[
12-
[0, -4, -1, 3, -5],
13-
[-5, 3, -1, -4, 0],
14-
],
15-
[
16-
[-2, 0, 6, 4, 4, -6],
17-
[-6, 4, 4, 6, 0, -2],
18-
],
19-
];
20-
cases.forEach(([input, output]) => {
21-
const result: number[] = [];
22-
const node = new ImmutableListNode(input, (v) => result.push(v));
23-
printLinkedListInReverse(node);
24-
25-
assertEquals(output, result);
26-
});
27-
});
1+
import { ImmutableListNode } from "./ImmutableListNode.ts";
2+
import printLinkedListInReverse from "./index.ts";
3+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
4+
5+
Deno.test("print-immutable-linked-list-in-reverse", () => {
6+
const cases = [
7+
[
8+
[1, 2, 3, 4],
9+
[4, 3, 2, 1],
10+
],
11+
[
12+
[0, -4, -1, 3, -5],
13+
[-5, 3, -1, -4, 0],
14+
],
15+
[
16+
[-2, 0, 6, 4, 4, -6],
17+
[-6, 4, 4, 6, 0, -2],
18+
],
19+
];
20+
cases.forEach(([input, output]) => {
21+
const result: number[] = [];
22+
const node = new ImmutableListNode(input, (v) => result.push(v));
23+
printLinkedListInReverse(node);
24+
25+
assertEquals(output, result);
26+
});
27+
});

0 commit comments

Comments
 (0)