模拟三个栈水题,可以练习结构体的使用

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
type TripleInOne struct {
a []int
index [3]int
size int
}

func Constructor(stackSize int) TripleInOne {
return TripleInOne {
make([]int, stackSize * 3 + 10),
[3]int{0, stackSize, stackSize * 2},
stackSize,
}
}

func (this *TripleInOne) Push(stackNum int, value int) {
if this.index[stackNum] == (stackNum + 1) * this.size {
return
}
this.a[this.index[stackNum]] = value
this.index[stackNum] ++
}


func (this *TripleInOne) Pop(stackNum int) int {
if this.IsEmpty(stackNum) {
return -1
}
ans := this.a[this.index[stackNum] - 1]
this.index[stackNum] --
return ans
}


func (this *TripleInOne) Peek(stackNum int) int {
if this.IsEmpty(stackNum) {
return -1
}
return this.a[this.index[stackNum] - 1]
}


func (this *TripleInOne) IsEmpty(stackNum int) bool {
//fmt.Println(stackNum)
if this.index[stackNum] > stackNum * this.size {
return false
}
return true
}


/**
* 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);
*/