Skip to content

Commit 4b73a6c

Browse files
committed
uniqueMorseRepresentations
1 parent 79f146b commit 4b73a6c

File tree

7 files changed

+182
-29
lines changed

7 files changed

+182
-29
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#### 介绍
44

5-
leetcode测试
5+
leetcode 测试
66

77
#### 软件架构
88

@@ -32,6 +32,8 @@ https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array
3232

3333
https://leetcode-cn.com/problems/move-zeroes/
3434

35+
https://leetcode-cn.com/problems/unique-morse-code-words/
36+
3537
#### 安装教程
3638

3739
1. 安装`deno`
@@ -68,7 +70,7 @@ deno task fmt
6870
deno task lint
6971
```
7072

71-
5.使用模块
73+
5.导入模块
7274

7375
```ts
7476
import {} from "https://gitee.com/masx200/leetcode-test/raw/master/mod.ts";
@@ -82,6 +84,28 @@ import {} from "https://github.com/masx200/leetcode-test/raw/master/mod.ts";
8284
import {} from "https://cdn.jsdelivr.net/gh/masx200/leetcode-test/mod.ts";
8385
```
8486

87+
6.使用举例
88+
89+
```ts
90+
import {
91+
ArrayToListNode,
92+
climbing_stairs,
93+
fei_bo_na_qi_shu_lie_lcof,
94+
fibonacci_Number,
95+
find_all_numbers_disappeared_in_an_array,
96+
html_Entity_Parser,
97+
ListNode,
98+
ListNodeToArray,
99+
merge_Sorted_Array,
100+
move_zeros,
101+
pow_x_n,
102+
que_shi_de_shu_zi_lcof,
103+
reverse_Linked_List,
104+
two_Sum,
105+
unique_morse_code_words,
106+
} from "./mod.ts";
107+
```
108+
85109
#### 参与贡献
86110

87111
1. Fork 本仓库

html-entity-parser/test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert } from "../deps.ts";
1+
import { assertEquals } from "../deps.ts";
22
import { html_Entity_Parser } from "../mod.ts";
33

44
Deno.test("html-entity-parser", () => {
@@ -25,9 +25,7 @@ Deno.test("html-entity-parser", () => {
2525
},
2626
];
2727

28-
assert(
29-
testData.every(
30-
({ input, output }) => html_Entity_Parser(input) === output,
31-
),
32-
);
28+
testData.forEach(function ({ input, output }) {
29+
assertEquals(html_Entity_Parser(input), output);
30+
});
3331
});

mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export {
2626
export { default as que_shi_de_shu_zi_lcof } from "./que-shi-de-shu-zi-lcof/index.ts";
2727
export { default as find_all_numbers_disappeared_in_an_array } from "./find-all-numbers-disappeared-in-an-array/index.ts";
2828
export { default as move_zeros } from "./move-zeroes/index.ts";
29+
export { default as unique_morse_code_words } from "./unique-morse-code-words/index.ts";

reverse-linked-list/test.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, equal } from "../deps.ts";
1+
import { assertEquals } from "../deps.ts";
22
import {
33
ArrayToListNode,
44
ListNodeToArray,
@@ -21,21 +21,14 @@ Deno.test("reverse-linked-list", () => {
2121
},
2222
];
2323

24-
assert(
25-
testData.every(
26-
({ input, output }) =>
27-
equal(
28-
ListNodeToArray(
29-
reverse_Linked_List(ArrayToListNode(input)),
30-
),
31-
output,
32-
) &&
33-
equal(
34-
ListNodeToArray(
35-
reverse_Linked_List(ArrayToListNode(output)),
36-
),
37-
input,
38-
),
39-
),
40-
);
24+
testData.forEach(function ({ input, output }) {
25+
assertEquals(
26+
ListNodeToArray(reverse_Linked_List(ArrayToListNode(input))),
27+
output,
28+
);
29+
assertEquals(
30+
ListNodeToArray(reverse_Linked_List(ArrayToListNode(output))),
31+
input,
32+
);
33+
});
4134
});

two-sum/test.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,33 @@ Deno.test("two-sum", () => {
1111
{ input: [[3, 2, 4], 6], output: [1, 2] },
1212
{
1313
input: [
14-
Array(25)
15-
.fill(0)
16-
.map((_v, i) => i),
14+
[
15+
0,
16+
1,
17+
2,
18+
3,
19+
4,
20+
5,
21+
6,
22+
7,
23+
8,
24+
9,
25+
10,
26+
11,
27+
12,
28+
13,
29+
14,
30+
15,
31+
16,
32+
17,
33+
18,
34+
19,
35+
20,
36+
21,
37+
22,
38+
23,
39+
24,
40+
],
1741
30,
1842
],
1943
output: [14, 16],

unique-morse-code-words/index.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export default function uniqueMorseRepresentations(words: string[]): number {
2+
const translations = words.map((word) =>
3+
Array.from(word)
4+
.map((v) => {
5+
const m = letterToMorse.get(v);
6+
7+
if (typeof m === "undefined") {
8+
throw Error("invalid word");
9+
}
10+
return m;
11+
})
12+
.join("")
13+
);
14+
15+
const seen = new Set<string>(translations);
16+
return seen.size;
17+
}
18+
const MORSE = [
19+
".-",
20+
"-...",
21+
"-.-.",
22+
"-..",
23+
".",
24+
"..-.",
25+
"--.",
26+
"....",
27+
"..",
28+
".---",
29+
"-.-",
30+
".-..",
31+
"--",
32+
"-.",
33+
"---",
34+
".--.",
35+
"--.-",
36+
".-.",
37+
"...",
38+
"-",
39+
"..-",
40+
"...-",
41+
".--",
42+
"-..-",
43+
"-.--",
44+
"--..",
45+
];
46+
const letterToMorse = new Map(
47+
MORSE.map((v, i) => [String.fromCharCode(i + "a".charCodeAt(0)), v]),
48+
);

unique-morse-code-words/test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { assertStrictEquals } from "../deps.ts";
2+
import { unique_morse_code_words } from "../mod.ts";
3+
4+
Deno.test("unique-morse-code-words", () => {
5+
const examples: {
6+
input: Parameters<typeof unique_morse_code_words>[0];
7+
output: ReturnType<typeof unique_morse_code_words>;
8+
}[] = [
9+
{ input: ["gin", "zen", "gig", "msg"], output: 2 },
10+
{ input: ["a"], output: 1 },
11+
{
12+
input: [
13+
"saenc",
14+
"saenca",
15+
"saencb",
16+
"saencc",
17+
"saencd",
18+
"saence",
19+
"saencf",
20+
"saencg",
21+
"saench",
22+
"saenci",
23+
"effnronmwnj",
24+
"effnronmwnk",
25+
"effnronmwnl",
26+
"effnronmwnm",
27+
"effnronmwnn",
28+
"effnronmwno",
29+
"effnronmwnp",
30+
"effnronmwnq",
31+
"effnronmwnr",
32+
"effnronmwns",
33+
],
34+
output: 20,
35+
},
36+
{
37+
input: [
38+
"saenc",
39+
"saenc",
40+
"saenc",
41+
"saenc",
42+
"saenc",
43+
"saenc",
44+
"saenc",
45+
"saenc",
46+
"saenc",
47+
"saenc",
48+
"effnronmwn",
49+
"effnronmwn",
50+
"effnronmwn",
51+
"effnronmwn",
52+
"effnronmwn",
53+
"effnronmwn",
54+
"effnronmwn",
55+
"effnronmwn",
56+
"effnronmwn",
57+
"effnronmwn",
58+
],
59+
output: 2,
60+
},
61+
];
62+
examples.forEach(({ input, output }) => {
63+
assertStrictEquals(unique_morse_code_words(input), output);
64+
});
65+
});

0 commit comments

Comments
 (0)