-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.go
57 lines (51 loc) · 1.18 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
type TripleInOne struct {
data []int
offset [3]int
stackSize int
}
func Constructor(stackSize int) TripleInOne {
total := stackSize * 3
data := make([]int, total)
offset := [3]int{}
for i := 0; i < 3; i++ {
offset[i] = i * stackSize
}
return TripleInOne{
data: data,
offset: offset,
stackSize: stackSize,
}
}
func (this *TripleInOne) Push(stackNum int, value int) {
i := this.offset[stackNum]
if i < (stackNum+1)*this.stackSize {
this.data[i] = value
this.offset[stackNum]++
}
}
func (this *TripleInOne) Pop(stackNum int) int {
i := this.offset[stackNum]
if i == stackNum*this.stackSize {
return -1
}
this.offset[stackNum]--
return this.data[i-1]
}
func (this *TripleInOne) Peek(stackNum int) int {
i := this.offset[stackNum]
if i == stackNum*this.stackSize {
return -1
}
return this.data[i-1]
}
func (this *TripleInOne) IsEmpty(stackNum int) bool {
return this.offset[stackNum] == stackNum*this.stackSize
}
/**
* Your TripleInOne object will be instantiated and called as such:
* obj := Constructor(stackSize);
* obj.Push(stackNum,value);
* param_2 := obj.Pop(stackNum);
* param_3 := obj.Peek(stackNum);
* param_4 := obj.IsEmpty(stackNum);
*/