-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0588-design-in-memory-file-system.js
More file actions
97 lines (84 loc) · 2.62 KB
/
0588-design-in-memory-file-system.js
File metadata and controls
97 lines (84 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* 588. Design In-Memory File System
* https://leetcode.com/problems/design-in-memory-file-system/
* Difficulty: Hard
*
* Design a data structure that simulates an in-memory file system.
*
* Implement the FileSystem class:
* - FileSystem() Initializes the object of the system.
* - List<String> ls(String path)
* - If path is a file path, returns a list that only contains this file's name.
* - If path is a directory path, returns the list of file and directory names in this directory.
* - The answer should in lexicographic order.
* - void mkdir(String path) Makes a new directory according to the given path. The given directory
* path does not exist. If the middle directories in the path do not exist, you should create
* them as well.
* - void addContentToFile(String filePath, String content)
* - If filePath does not exist, creates that file containing given content.
* - If filePath already exists, appends the given content to original content.
* - String readContentFromFile(String filePath) Returns the content in the file at filePath.
*/
var FileSystem = function() {
this.root = new Map();
};
/**
* @param {string} path
* @return {string[]}
*/
FileSystem.prototype.ls = function(path) {
const parts = path === '/' ? [] : path.split('/').slice(1);
let current = this.root;
for (const part of parts) {
current = current.get(part);
}
if (typeof current === 'string') {
return [parts[parts.length - 1]];
}
return Array.from(current.keys()).sort();
};
/**
* @param {string} path
* @return {void}
*/
FileSystem.prototype.mkdir = function(path) {
const parts = path.split('/').slice(1);
let current = this.root;
for (const part of parts) {
if (!current.has(part)) {
current.set(part, new Map());
}
current = current.get(part);
}
};
/**
* @param {string} filePath
* @param {string} content
* @return {void}
*/
FileSystem.prototype.addContentToFile = function(filePath, content) {
const parts = filePath.split('/').slice(1);
const fileName = parts.pop();
let current = this.root;
for (const part of parts) {
if (!current.has(part)) {
current.set(part, new Map());
}
current = current.get(part);
}
const existingContent = current.get(fileName) || '';
current.set(fileName, existingContent + content);
};
/**
* @param {string} filePath
* @return {string}
*/
FileSystem.prototype.readContentFromFile = function(filePath) {
const parts = filePath.split('/').slice(1);
const fileName = parts.pop();
let current = this.root;
for (const part of parts) {
current = current.get(part);
}
return current.get(fileName);
};