Skip to content

File tree

11 files changed

+171
-58
lines changed

11 files changed

+171
-58
lines changed

.vscode/settings.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"deno.enable": true,
3-
"deno.lint": true,
4-
"deno.unstable": true,
5-
"deno.config": "deno.json",
6-
"deno.importMap": "import_map.json"
2+
"deno.enable": true,
3+
"deno.lint": true,
4+
"deno.unstable": true,
5+
"deno.config": "deno.json",
6+
"deno.importMap": "import_map.json"
77
}

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ leetcode测试
88

99
软件架构说明
1010

11+
包含的内容如下
12+
13+
https://leetcode-cn.com/problems/reverse-linked-list/
14+
15+
https://leetcode-cn.com/problems/html-entity-parser/
16+
1117
#### 安装教程
1218

1319
1. 安装`deno`
@@ -23,7 +29,7 @@ https://deno.land/x/udd#installation
2329
1. 测试
2430

2531
```
26-
deno test
32+
deno task test
2733
```
2834

2935
2.升级依赖
@@ -32,6 +38,12 @@ deno test
3238
deno task udd
3339
```
3440

41+
3.格式化
42+
43+
```
44+
deno task fmt
45+
```
46+
3547
#### 参与贡献
3648

3749
1. Fork 本仓库

deno.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
{
2-
"importMap": "import_map.json",
3-
"tasks": {
4-
"udd": "udd '*.ts'",
5-
"install-udd": "deno install -A -f -n udd https://deno.land/x/[email protected]/main.ts"
6-
}
2+
"importMap": "import_map.json",
3+
"tasks": {
4+
"udd": "udd '*.ts'",
5+
"install-udd": "deno install -A -f -n udd https://deno.land/x/[email protected]/main.ts",
6+
"fmt": "deno fmt --config deno.json",
7+
"test": "deno test --config deno.json"
8+
},
9+
"fmt": {
10+
"options": {
11+
"indentWidth": 4,
12+
"singleQuote": false
13+
}
14+
}
715
}

deps.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
2-
export { assert };
1+
import {
2+
assert,
3+
equal,
4+
} from "https://deno.land/[email protected]/testing/asserts.ts";
5+
export { assert, equal };

fmt.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
deno fmt
1+
deno fmt --config deno.json

html-entity-parser/index.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
/*https://leetcode-cn.com/problems/html-entity-parser/*/
2-
export default entityParser;
3-
4-
function entityParser(text: string): string {
5-
return text.replace(entityRegExp, replacer);
1+
/**https://leetcode-cn.com/problems/html-entity-parser/*/
2+
export default function entityParser(text: string): string {
3+
return text.replace(entityRegExp, replacer);
64
}
75
const replacer = (a: string) => {
8-
const value = translator.get(a);
9-
return value ?? a;
6+
const value = translator.get(a);
7+
return value ?? a;
108
};
119
const translator = new Map<string, string>([
12-
["&quot;", '"'],
13-
["&apos;", "'"],
14-
["&amp;", "&"],
15-
["&gt;", ">"],
16-
["&lt;", "<"],
17-
["&frasl;", "/"],
10+
["&quot;", '"'],
11+
["&apos;", "'"],
12+
["&amp;", "&"],
13+
["&gt;", ">"],
14+
["&lt;", "<"],
15+
["&frasl;", "/"],
1816
]);
1917
const entityRegExp = /\&[a-z]+\;/g;

html-entity-parser/test.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
import { assert } from "../deps.ts";
2-
import entityParser from "./index.ts";
2+
import { htmlEntityParser } from "../mod.ts";
33

44
Deno.test("html-entity-parser", () => {
5-
const testData: { input: string; output: string }[] = [
6-
{
7-
input: "&amp; is an HTML entity but &ambassador; is not.",
8-
output: "& is an HTML entity but &ambassador; is not.",
9-
},
10-
{
11-
input: "and I quote: &quot;...&quot;",
12-
output: 'and I quote: "..."',
13-
},
14-
{
15-
input: "Stay home! Practice on Leetcode :)",
16-
output: "Stay home! Practice on Leetcode :)",
17-
},
18-
{
19-
input: "x &gt; y &amp;&amp; x &lt; y is always false",
20-
output: "x > y && x < y is always false",
21-
},
22-
{
23-
input: "leetcode.com&frasl;problemset&frasl;all",
24-
output: "leetcode.com/problemset/all",
25-
},
26-
];
5+
const testData: { input: string; output: string }[] = [
6+
{
7+
input: "&amp; is an HTML entity but &ambassador; is not.",
8+
output: "& is an HTML entity but &ambassador; is not.",
9+
},
10+
{
11+
input: "and I quote: &quot;...&quot;",
12+
output: 'and I quote: "..."',
13+
},
14+
{
15+
input: "Stay home! Practice on Leetcode :)",
16+
output: "Stay home! Practice on Leetcode :)",
17+
},
18+
{
19+
input: "x &gt; y &amp;&amp; x &lt; y is always false",
20+
output: "x > y && x < y is always false",
21+
},
22+
{
23+
input: "leetcode.com&frasl;problemset&frasl;all",
24+
output: "leetcode.com/problemset/all",
25+
},
26+
];
2727

28-
assert(
29-
testData.every(({ input, output }) => entityParser(input) === output),
30-
);
28+
assert(
29+
testData.every(
30+
({ input, output }) => htmlEntityParser(input) === output,
31+
),
32+
);
3133
});

import_map.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"imports": {
3-
"asserts": "https://deno.land/[email protected]/testing/asserts.ts"
4-
}
2+
"imports": {
3+
}
54
}

mod.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
import entityParser from "./html-entity-parser/index.ts";
2-
export { entityParser };
1+
import htmlEntityParser from "./html-entity-parser/index.ts";
2+
export { htmlEntityParser };
3+
export * from "./reverse-linked-list/index.ts";
4+
import reverseLinkedList from "./reverse-linked-list/index.ts";
5+
export { reverseLinkedList };

reverse-linked-list/index.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// My code goes here
2+
/**
3+
* Definition for singly-linked list.
4+
* class ListNode {
5+
* val: number
6+
* next: ListNode | null
7+
* constructor(val?: number, next?: ListNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.next = (next===undefined ? null : next)
10+
* }
11+
* }
12+
*/
13+
export class ListNode {
14+
val: number;
15+
next: ListNode | null;
16+
constructor(val?: number, next?: ListNode | null) {
17+
this.val = val === undefined ? 0 : val;
18+
this.next = next === undefined ? null : next;
19+
}
20+
}
21+
/**https://leetcode-cn.com/problems/reverse-linked-list/ */
22+
export default function reverseLinkedList(
23+
head: ListNode | null,
24+
): ListNode | null {
25+
if (null === head) {
26+
return null;
27+
}
28+
29+
return ArrayToListNode(Array.from(ListNodeToArray(head)).reverse());
30+
}
31+
32+
export function ArrayToListNode(array: Array<number>): ListNode | null {
33+
if (array.length === 0) {
34+
return null;
35+
}
36+
const list = new ListNode(array[0]);
37+
array.slice(1).reduce((p, v) => {
38+
const l = new ListNode(v);
39+
p.next = l;
40+
return l;
41+
}, list);
42+
return list;
43+
}
44+
export function ListNodeToArray(list: ListNode | null): Array<number> {
45+
if (list === null) {
46+
return [];
47+
}
48+
const array: Array<number> = [];
49+
let temp: ListNode | null = list;
50+
while (temp) {
51+
array.push(temp.val);
52+
temp = temp.next;
53+
}
54+
return array;
55+
}

0 commit comments

Comments
 (0)