-
Notifications
You must be signed in to change notification settings - Fork 502
/
b.go
54 lines (46 loc) · 920 Bytes
/
b.go
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
package main
import "strings"
// github.com/EndlessCheng/codeforces-go
type FileSystem struct{}
type node struct {
ch map[string]*node
val int
}
var rt *node
func Constructor() (_ FileSystem) {
rt = &node{ch: map[string]*node{}}
return
}
func (FileSystem) CreatePath(path string, value int) (ans bool) {
sp := strings.Split(path[1:], "/")
n := len(sp)
o := rt
for _, s := range sp[:n-1] {
if o.ch[s] == nil {
return
}
o = o.ch[s]
}
end := sp[n-1]
if o.ch[end] != nil {
return
}
o.ch[end] = &node{map[string]*node{}, value}
return true
}
func (FileSystem) Get(path string) (ans int) {
o := rt
for _, s := range strings.Split(path[1:], "/") {
if o.ch[s] == nil {
return -1
}
o = o.ch[s]
}
return o.val
}
/**
* Your FileSystem object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.CreatePath(path,value);
* param_2 := obj.Get(path);
*/