Skip to content

Commit e3b3076

Browse files
committed
https://leetcode.cn/problems/design-file-system/
1 parent 0332db0 commit e3b3076

File tree

5 files changed

+91
-27
lines changed

5 files changed

+91
-27
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/design-file-system/
14+
1315
https://leetcode.cn/problems/find-duplicate-file-in-system/
1416

1517
https://leetcode.cn/problems/design-in-memory-file-system/

design-file-system/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default class FileSystem {
2+
#map = new Map<string, number | undefined>();
3+
constructor() {
4+
this.#map.set("/", undefined);
5+
}
6+
createPath(path: string, value: number): boolean {
7+
if (this.#map.has(path)) return false;
8+
const parent = path.split("/").slice(0, -1).join("/") || "/";
9+
if (!this.#map.has(parent)) return false;
10+
11+
const ps = path.split("/");
12+
for (let index = 0; index < ps.length - 1; index++) {
13+
const sub = ps.slice(0, index + 1).join("/") || "/";
14+
15+
const node = this.#map.get(sub);
16+
this.#map.set(sub, node);
17+
}
18+
this.#map.set(path, value);
19+
return true;
20+
}
21+
get(path: string): number {
22+
// console.log(this);
23+
return this.#map.get(path) ?? -1;
24+
}
25+
}

design-file-system/test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import { runScript } from "leetcode-class";
3+
import FileSystem from "./index.ts";
4+
Deno.test("design-file-system", () => {
5+
assertEquals(
6+
runScript(
7+
["FileSystem", "createPath", "get"],
8+
[[], ["/a", 1], ["/a"]],
9+
FileSystem,
10+
),
11+
[null, true, 1],
12+
);
13+
});
14+
Deno.test("design-file-system", () => {
15+
assertEquals(
16+
runScript(
17+
[
18+
"FileSystem",
19+
"createPath",
20+
"createPath",
21+
"get",
22+
"createPath",
23+
"get",
24+
],
25+
[
26+
[],
27+
["/leet", 1],
28+
["/leet/code", 2],
29+
["/leet/code"],
30+
["/c/d", 1],
31+
["/c"],
32+
],
33+
FileSystem,
34+
),
35+
[null, true, true, 2, false, -1],
36+
);
37+
});

design-in-memory-file-system/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class Node {
77
}
88
export { Node };
99
class FileSystem {
10-
paths = new Map<string, Node>();
10+
#paths = new Map<string, Node>();
1111
constructor() {
12-
this.paths.set("/", new Node("folder"));
12+
this.#paths.set("/", new Node("folder"));
1313
}
1414
ls(path: string): string[] {
15-
const node = this.paths.get(path);
15+
const node = this.#paths.get(path);
1616
if (node) {
1717
if (node.type === "file") {
1818
return [path.split("/").at(-1) ?? ""];
@@ -24,33 +24,33 @@ class FileSystem {
2424
}
2525
}
2626
mkdir(path: string): void {
27-
if (this.paths.has(path)) return;
27+
if (this.#paths.has(path)) return;
2828

2929
const ps = path.split("/");
3030

3131
for (let index = 0; index < ps.length - 1; index++) {
3232
const element = ps[index + 1];
3333
const sub = ps.slice(0, index + 1).join("/") || "/";
3434

35-
const node = this.paths.get(sub) ?? new Node("folder");
36-
this.paths.set(sub, node);
35+
const node = this.#paths.get(sub) ?? new Node("folder");
36+
this.#paths.set(sub, node);
3737

3838
node.children.add(element);
3939
}
40-
this.paths.set(path, new Node("folder"));
40+
this.#paths.set(path, new Node("folder"));
4141
}
4242
addContentToFile(filePath: string, content: string) {
43-
if (!this.paths.has(filePath)) {
43+
if (!this.#paths.has(filePath)) {
4444
this.mkdir(filePath);
4545
}
46-
const node = this.paths.get(filePath);
46+
const node = this.#paths.get(filePath);
4747
if (node) {
4848
node.type = "file";
4949
node.content += content;
5050
}
5151
}
5252
readContentFromFile(filePath: string): string {
53-
return this.paths.get(filePath)?.content ?? "";
53+
return this.#paths.get(filePath)?.content ?? "";
5454
}
5555
}
5656
export default FileSystem;
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
export default function findDuplicate(paths: string[]): string[][] {
2-
const map = new Map<string, string[]>();
3-
4-
for (const ps of paths) {
5-
const [directory, ...contents] = ps.split(" ");
6-
7-
for (const con of contents) {
8-
const [name, content] = con.split(/\(|\)/g).filter(Boolean);
9-
10-
const arr = map.get(content) ?? [];
11-
arr.push(directory + "/" + name);
12-
map.set(content, arr);
13-
}
14-
}
15-
16-
return [...map.values()].filter((a) => a.length > 1);
17-
}
1+
export default function findDuplicate(paths: string[]): string[][] {
2+
const map = new Map<string, string[]>();
3+
4+
for (const ps of paths) {
5+
const [directory, ...contents] = ps.split(" ");
6+
7+
for (const con of contents) {
8+
const [name, content] = con.split(/\(|\)/g).filter(Boolean);
9+
10+
const arr = map.get(content) ?? [];
11+
arr.push(directory + "/" + name);
12+
map.set(content, arr);
13+
}
14+
}
15+
16+
return [...map.values()].filter((a) => a.length > 1);
17+
}

0 commit comments

Comments
 (0)