Skip to content

Commit ad18836

Browse files
committed
feat: add solutions to lc problem: No.0588
No.0588.Design In-Memory File System
1 parent 7dd1fec commit ad18836

File tree

5 files changed

+721
-0
lines changed

5 files changed

+721
-0
lines changed

solution/0500-0599/0588.Design In-Memory File System/README.md

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,266 @@ fileSystem.readContentFromFile("/a/b/c/d"); // 返回 "hello"</pre>
7171

7272
<!-- 这里可写通用的实现逻辑 -->
7373

74+
**方法一:前缀树**
75+
76+
哈希表实现前缀树。
77+
7478
<!-- tabs:start -->
7579

7680
### **Python3**
7781

7882
<!-- 这里可写当前语言的特殊实现逻辑 -->
7983

8084
```python
85+
class Trie:
86+
def __init__(self):
87+
self.name = None
88+
self.isFile = False
89+
self.content = []
90+
self.children = {}
91+
92+
def insert(self, path, isFile):
93+
node = self
94+
ps = path.split('/')
95+
for p in ps[1:]:
96+
if p not in node.children:
97+
node.children[p] = Trie()
98+
node = node.children[p]
99+
node.isFile = isFile
100+
if isFile:
101+
node.name = ps[-1]
102+
return node
103+
104+
def search(self, path):
105+
node = self
106+
if path == '/':
107+
return node
108+
ps = path.split('/')
109+
for p in ps[1:]:
110+
if p not in node.children:
111+
return None
112+
node = node.children[p]
113+
return node
114+
115+
116+
class FileSystem:
117+
118+
def __init__(self):
119+
self.root = Trie()
120+
121+
def ls(self, path: str) -> List[str]:
122+
node = self.root.search(path)
123+
if node is None:
124+
return []
125+
if node.isFile:
126+
return [node.name]
127+
return sorted(node.children.keys())
81128

129+
def mkdir(self, path: str) -> None:
130+
self.root.insert(path, False)
131+
132+
def addContentToFile(self, filePath: str, content: str) -> None:
133+
node = self.root.insert(filePath, True)
134+
node.content.append(content)
135+
136+
def readContentFromFile(self, filePath: str) -> str:
137+
node = self.root.search(filePath)
138+
return ''.join(node.content)
139+
140+
141+
# Your FileSystem object will be instantiated and called as such:
142+
# obj = FileSystem()
143+
# param_1 = obj.ls(path)
144+
# obj.mkdir(path)
145+
# obj.addContentToFile(filePath,content)
146+
# param_4 = obj.readContentFromFile(filePath)
82147
```
83148

84149
### **Java**
85150

86151
<!-- 这里可写当前语言的特殊实现逻辑 -->
87152

88153
```java
154+
class Trie {
155+
String name;
156+
boolean isFile;
157+
StringBuilder content = new StringBuilder();
158+
Map<String, Trie> children = new HashMap<>();
159+
160+
Trie insert(String path, boolean isFile) {
161+
Trie node = this;
162+
String[] ps = path.split("/");
163+
for (int i = 1; i < ps.length; ++i) {
164+
String p = ps[i];
165+
if (!node.children.containsKey(p)) {
166+
node.children.put(p, new Trie());
167+
}
168+
node = node.children.get(p);
169+
}
170+
node.isFile = isFile;
171+
if (isFile) {
172+
node.name = ps[ps.length - 1];
173+
}
174+
return node;
175+
}
176+
177+
Trie search(String path) {
178+
Trie node = this;
179+
String[] ps = path.split("/");
180+
for (int i = 1; i < ps.length; ++i) {
181+
String p = ps[i];
182+
if (!node.children.containsKey(p)) {
183+
return null;
184+
}
185+
node = node.children.get(p);
186+
}
187+
return node;
188+
}
189+
}
190+
191+
class FileSystem {
192+
private Trie root = new Trie();
193+
194+
public FileSystem() {
195+
196+
}
197+
198+
public List<String> ls(String path) {
199+
List<String> ans = new ArrayList<>();
200+
Trie node = root.search(path);
201+
if (node == null) {
202+
return ans;
203+
}
204+
if (node.isFile) {
205+
ans.add(node.name);
206+
return ans;
207+
}
208+
for (String v : node.children.keySet()) {
209+
ans.add(v);
210+
}
211+
Collections.sort(ans);
212+
return ans;
213+
}
214+
215+
public void mkdir(String path) {
216+
root.insert(path, false);
217+
}
218+
219+
public void addContentToFile(String filePath, String content) {
220+
Trie node = root.insert(filePath, true);
221+
node.content.append(content);
222+
}
223+
224+
public String readContentFromFile(String filePath) {
225+
Trie node = root.search(filePath);
226+
return node.content.toString();
227+
}
228+
}
229+
230+
/**
231+
* Your FileSystem object will be instantiated and called as such:
232+
* FileSystem obj = new FileSystem();
233+
* List<String> param_1 = obj.ls(path);
234+
* obj.mkdir(path);
235+
* obj.addContentToFile(filePath,content);
236+
* String param_4 = obj.readContentFromFile(filePath);
237+
*/
238+
```
239+
240+
### **Go**
241+
242+
```go
243+
type Trie struct {
244+
name string
245+
isFile bool
246+
content strings.Builder
247+
children map[string]*Trie
248+
}
249+
250+
func newTrie() *Trie {
251+
m := map[string]*Trie{}
252+
return &Trie{children: m}
253+
}
254+
255+
func (this *Trie) insert(path string, isFile bool) *Trie {
256+
node := this
257+
ps := strings.Split(path, "/")
258+
for _, p := range ps[1:] {
259+
if _, ok := node.children[p]; !ok {
260+
node.children[p] = newTrie()
261+
}
262+
node, _ = node.children[p]
263+
}
264+
node.isFile = isFile
265+
if isFile {
266+
node.name = ps[len(ps)-1]
267+
}
268+
return node
269+
}
270+
271+
func (this *Trie) search(path string) *Trie {
272+
if path == "/" {
273+
return this
274+
}
275+
node := this
276+
ps := strings.Split(path, "/")
277+
for _, p := range ps[1:] {
278+
if _, ok := node.children[p]; !ok {
279+
return nil
280+
}
281+
node, _ = node.children[p]
282+
}
283+
return node
284+
}
285+
286+
type FileSystem struct {
287+
root *Trie
288+
}
289+
290+
func Constructor() FileSystem {
291+
root := newTrie()
292+
return FileSystem{root}
293+
}
294+
295+
func (this *FileSystem) Ls(path string) []string {
296+
var ans []string
297+
node := this.root.search(path)
298+
if node == nil {
299+
return ans
300+
}
301+
if node.isFile {
302+
ans = append(ans, node.name)
303+
return ans
304+
}
305+
for v := range node.children {
306+
ans = append(ans, v)
307+
}
308+
sort.Strings(ans)
309+
return ans
310+
}
311+
312+
func (this *FileSystem) Mkdir(path string) {
313+
this.root.insert(path, false)
314+
}
315+
316+
func (this *FileSystem) AddContentToFile(filePath string, content string) {
317+
node := this.root.insert(filePath, true)
318+
node.content.WriteString(content)
319+
}
320+
321+
func (this *FileSystem) ReadContentFromFile(filePath string) string {
322+
node := this.root.search(filePath)
323+
return node.content.String()
324+
}
89325

326+
/**
327+
* Your FileSystem object will be instantiated and called as such:
328+
* obj := Constructor();
329+
* param_1 := obj.Ls(path);
330+
* obj.Mkdir(path);
331+
* obj.AddContentToFile(filePath,content);
332+
* param_4 := obj.ReadContentFromFile(filePath);
333+
*/
90334
```
91335

92336
### **...**

0 commit comments

Comments
 (0)