forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
65 lines (57 loc) · 1.2 KB
/
Solution.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
55
56
57
58
59
60
61
62
63
64
65
type Trie struct {
children map[string]*Trie
v int
}
func newTrie() *Trie {
m := map[string]*Trie{}
return &Trie{children: m}
}
func (this *Trie) insert(w string, v int) bool {
node := this
ps := strings.Split(w, "/")
for _, p := range ps[1 : len(ps)-1] {
if _, ok := node.children[p]; !ok {
return false
}
node, _ = node.children[p]
}
x := ps[len(ps)-1]
if _, ok := node.children[x]; ok {
return false
}
node.children[x] = newTrie()
node, _ = node.children[x]
node.v = v
return true
}
func (this *Trie) search(w string) int {
node := this
for _, p := range strings.Split(w, "/")[1:] {
if _, ok := node.children[p]; !ok {
return -1
}
node, _ = node.children[p]
}
if node.v == 0 {
return -1
}
return node.v
}
type FileSystem struct {
trie *Trie
}
func Constructor() FileSystem {
return FileSystem{newTrie()}
}
func (this *FileSystem) CreatePath(path string, value int) bool {
return this.trie.insert(path, value)
}
func (this *FileSystem) Get(path string) int {
return this.trie.search(path)
}
/**
* Your FileSystem object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.CreatePath(path,value);
* param_2 := obj.Get(path);
*/