-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (41 loc) · 883 Bytes
/
main.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
package main
func main() {
}
type SnapshotArray struct {
mp map[int]map[int]int
snapID int
}
func Constructor(length int) SnapshotArray {
return SnapshotArray{
mp: make(map[int]map[int]int),
}
}
func (this *SnapshotArray) Set(index int, val int) {
if _, ok := this.mp[this.snapID]; !ok {
this.mp[this.snapID] = make(map[int]int)
}
this.mp[this.snapID][index] = val
}
func (this *SnapshotArray) Snap() int {
defer func() {
this.snapID++
}()
return this.snapID
}
func (this *SnapshotArray) Get(index int, snap_id int) int {
for i := snap_id; i >= 0; i-- {
if val, ok := this.mp[i]; ok {
if v, ok := val[index]; ok {
return v
}
}
}
return 0
}
/**
* Your SnapshotArray object will be instantiated and called as such:
* obj := Constructor(length);
* obj.Set(index,val);
* param_2 := obj.Snap();
* param_3 := obj.Get(index,snap_id);
*/