Skip to content

Commit 30b657b

Browse files
committed
feat: add solutions to lc problem: No.1166
No.1166.Design File System
1 parent cf9d2c6 commit 30b657b

File tree

5 files changed

+516
-2
lines changed

5 files changed

+516
-2
lines changed

solution/1100-1199/1166.Design File System/README.md

Lines changed: 176 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,197 @@ fileSystem.get("/c"); // 返回 -1 因为该路径不存在。
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69+
**方法一:前缀树**
70+
71+
哈希表实现前缀树。
72+
6973
<!-- tabs:start -->
7074

7175
### **Python3**
7276

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

7579
```python
76-
80+
class Trie:
81+
def __init__(self):
82+
self.children = {}
83+
self.v = 0
84+
85+
def insert(self, w, v):
86+
node = self
87+
ps = w.split('/')
88+
for p in ps[1:-1]:
89+
if p not in node.children:
90+
return False
91+
node = node.children[p]
92+
if ps[-1] in node.children:
93+
return False
94+
node.children[ps[-1]] = Trie()
95+
node = node.children[ps[-1]]
96+
node.v = v
97+
return True
98+
99+
def search(self, w):
100+
node = self
101+
for p in w.split('/')[1:]:
102+
if p not in node.children:
103+
return -1
104+
node = node.children[p]
105+
return node.v or -1
106+
107+
108+
class FileSystem:
109+
110+
def __init__(self):
111+
self.trie = Trie()
112+
113+
def createPath(self, path: str, value: int) -> bool:
114+
return self.trie.insert(path, value)
115+
116+
def get(self, path: str) -> int:
117+
return self.trie.search(path)
118+
119+
120+
# Your FileSystem object will be instantiated and called as such:
121+
# obj = FileSystem()
122+
# param_1 = obj.createPath(path,value)
123+
# param_2 = obj.get(path)
77124
```
78125

79126
### **Java**
80127

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

83130
```java
131+
class Trie {
132+
Map<String, Trie> children = new HashMap<>();
133+
int v;
134+
135+
boolean insert(String w, int v) {
136+
Trie node = this;
137+
String[] ps = w.split("/");
138+
for (int i = 1; i < ps.length - 1; ++i) {
139+
String p = ps[i];
140+
if (!node.children.containsKey(p)) {
141+
return false;
142+
}
143+
node = node.children.get(p);
144+
}
145+
if (node.children.containsKey(ps[ps.length - 1])) {
146+
return false;
147+
}
148+
node.children.put(ps[ps.length - 1], new Trie());
149+
node = node.children.get(ps[ps.length - 1]);
150+
node.v = v;
151+
return true;
152+
}
153+
154+
int search(String w) {
155+
Trie node = this;
156+
String[] ps = w.split("/");
157+
for (int i = 1; i < ps.length; ++i) {
158+
String p = ps[i];
159+
if (!node.children.containsKey(p)) {
160+
return -1;
161+
}
162+
node = node.children.get(p);
163+
}
164+
return node.v == 0 ? -1 : node.v;
165+
}
166+
}
167+
168+
class FileSystem {
169+
private Trie trie = new Trie();
170+
171+
public FileSystem() {
172+
173+
}
174+
175+
public boolean createPath(String path, int value) {
176+
return trie.insert(path, value);
177+
}
178+
179+
public int get(String path) {
180+
return trie.search(path);
181+
}
182+
}
183+
184+
/**
185+
* Your FileSystem object will be instantiated and called as such:
186+
* FileSystem obj = new FileSystem();
187+
* boolean param_1 = obj.createPath(path,value);
188+
* int param_2 = obj.get(path);
189+
*/
190+
```
84191

192+
### **Go**
193+
194+
```go
195+
type Trie struct {
196+
children map[string]*Trie
197+
v int
198+
}
199+
200+
func newTrie() *Trie {
201+
m := map[string]*Trie{}
202+
return &Trie{children: m}
203+
}
204+
205+
func (this *Trie) insert(w string, v int) bool {
206+
node := this
207+
ps := strings.Split(w, "/")
208+
for _, p := range ps[1 : len(ps)-1] {
209+
if _, ok := node.children[p]; !ok {
210+
return false
211+
}
212+
node, _ = node.children[p]
213+
}
214+
x := ps[len(ps)-1]
215+
if _, ok := node.children[x]; ok {
216+
return false
217+
}
218+
node.children[x] = newTrie()
219+
node, _ = node.children[x]
220+
node.v = v
221+
return true
222+
}
223+
224+
func (this *Trie) search(w string) int {
225+
node := this
226+
for _, p := range strings.Split(w, "/")[1:] {
227+
if _, ok := node.children[p]; !ok {
228+
return -1
229+
}
230+
node, _ = node.children[p]
231+
}
232+
if node.v == 0 {
233+
return -1
234+
}
235+
return node.v
236+
}
237+
238+
type FileSystem struct {
239+
trie *Trie
240+
}
241+
242+
func Constructor() FileSystem {
243+
return FileSystem{newTrie()}
244+
}
245+
246+
func (this *FileSystem) CreatePath(path string, value int) bool {
247+
return this.trie.insert(path, value)
248+
}
249+
250+
func (this *FileSystem) Get(path string) int {
251+
return this.trie.search(path)
252+
}
253+
254+
/**
255+
* Your FileSystem object will be instantiated and called as such:
256+
* obj := Constructor();
257+
* param_1 := obj.CreatePath(path,value);
258+
* param_2 := obj.Get(path);
259+
*/
85260
```
86261

87262
### **...**

solution/1100-1199/1166.Design File System/README_EN.md

Lines changed: 172 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,184 @@ fileSystem.get(&quot;/c&quot;); // return -1 because this path doesn&#39;t exist
6565
### **Python3**
6666

6767
```python
68-
68+
class Trie:
69+
def __init__(self):
70+
self.children = {}
71+
self.v = 0
72+
73+
def insert(self, w, v):
74+
node = self
75+
ps = w.split('/')
76+
for p in ps[1:-1]:
77+
if p not in node.children:
78+
return False
79+
node = node.children[p]
80+
if ps[-1] in node.children:
81+
return False
82+
node.children[ps[-1]] = Trie()
83+
node = node.children[ps[-1]]
84+
node.v = v
85+
return True
86+
87+
def search(self, w):
88+
node = self
89+
for p in w.split('/')[1:]:
90+
if p not in node.children:
91+
return -1
92+
node = node.children[p]
93+
return node.v or -1
94+
95+
96+
class FileSystem:
97+
98+
def __init__(self):
99+
self.trie = Trie()
100+
101+
def createPath(self, path: str, value: int) -> bool:
102+
return self.trie.insert(path, value)
103+
104+
def get(self, path: str) -> int:
105+
return self.trie.search(path)
106+
107+
108+
# Your FileSystem object will be instantiated and called as such:
109+
# obj = FileSystem()
110+
# param_1 = obj.createPath(path,value)
111+
# param_2 = obj.get(path)
69112
```
70113

71114
### **Java**
72115

73116
```java
117+
class Trie {
118+
Map<String, Trie> children = new HashMap<>();
119+
int v;
120+
121+
boolean insert(String w, int v) {
122+
Trie node = this;
123+
String[] ps = w.split("/");
124+
for (int i = 1; i < ps.length - 1; ++i) {
125+
String p = ps[i];
126+
if (!node.children.containsKey(p)) {
127+
return false;
128+
}
129+
node = node.children.get(p);
130+
}
131+
if (node.children.containsKey(ps[ps.length - 1])) {
132+
return false;
133+
}
134+
node.children.put(ps[ps.length - 1], new Trie());
135+
node = node.children.get(ps[ps.length - 1]);
136+
node.v = v;
137+
return true;
138+
}
139+
140+
int search(String w) {
141+
Trie node = this;
142+
String[] ps = w.split("/");
143+
for (int i = 1; i < ps.length; ++i) {
144+
String p = ps[i];
145+
if (!node.children.containsKey(p)) {
146+
return -1;
147+
}
148+
node = node.children.get(p);
149+
}
150+
return node.v == 0 ? -1 : node.v;
151+
}
152+
}
153+
154+
class FileSystem {
155+
private Trie trie = new Trie();
156+
157+
public FileSystem() {
158+
159+
}
160+
161+
public boolean createPath(String path, int value) {
162+
return trie.insert(path, value);
163+
}
164+
165+
public int get(String path) {
166+
return trie.search(path);
167+
}
168+
}
169+
170+
/**
171+
* Your FileSystem object will be instantiated and called as such:
172+
* FileSystem obj = new FileSystem();
173+
* boolean param_1 = obj.createPath(path,value);
174+
* int param_2 = obj.get(path);
175+
*/
176+
```
74177

178+
### **Go**
179+
180+
```go
181+
type Trie struct {
182+
children map[string]*Trie
183+
v int
184+
}
185+
186+
func newTrie() *Trie {
187+
m := map[string]*Trie{}
188+
return &Trie{children: m}
189+
}
190+
191+
func (this *Trie) insert(w string, v int) bool {
192+
node := this
193+
ps := strings.Split(w, "/")
194+
for _, p := range ps[1 : len(ps)-1] {
195+
if _, ok := node.children[p]; !ok {
196+
return false
197+
}
198+
node, _ = node.children[p]
199+
}
200+
x := ps[len(ps)-1]
201+
if _, ok := node.children[x]; ok {
202+
return false
203+
}
204+
node.children[x] = newTrie()
205+
node, _ = node.children[x]
206+
node.v = v
207+
return true
208+
}
209+
210+
func (this *Trie) search(w string) int {
211+
node := this
212+
for _, p := range strings.Split(w, "/")[1:] {
213+
if _, ok := node.children[p]; !ok {
214+
return -1
215+
}
216+
node, _ = node.children[p]
217+
}
218+
if node.v == 0 {
219+
return -1
220+
}
221+
return node.v
222+
}
223+
224+
type FileSystem struct {
225+
trie *Trie
226+
}
227+
228+
func Constructor() FileSystem {
229+
return FileSystem{newTrie()}
230+
}
231+
232+
func (this *FileSystem) CreatePath(path string, value int) bool {
233+
return this.trie.insert(path, value)
234+
}
235+
236+
func (this *FileSystem) Get(path string) int {
237+
return this.trie.search(path)
238+
}
239+
240+
/**
241+
* Your FileSystem object will be instantiated and called as such:
242+
* obj := Constructor();
243+
* param_1 := obj.CreatePath(path,value);
244+
* param_2 := obj.Get(path);
245+
*/
75246
```
76247

77248
### **...**

0 commit comments

Comments
 (0)