-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.go
45 lines (39 loc) · 889 Bytes
/
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
type SortedStack struct {
stk []int
}
func Constructor() SortedStack {
return SortedStack{}
}
func (this *SortedStack) Push(val int) {
t := make([]int, 0)
for len(this.stk) > 0 && this.stk[len(this.stk)-1] < val {
t = append(t, this.stk[len(this.stk)-1])
this.stk = this.stk[:len(this.stk)-1]
}
this.stk = append(this.stk, val)
for i := len(t) - 1; i >= 0; i-- {
this.stk = append(this.stk, t[i])
}
}
func (this *SortedStack) Pop() {
if !this.IsEmpty() {
this.stk = this.stk[:len(this.stk)-1]
}
}
func (this *SortedStack) Peek() int {
if this.IsEmpty() {
return -1
}
return this.stk[len(this.stk)-1]
}
func (this *SortedStack) IsEmpty() bool {
return len(this.stk) == 0
}
/**
* Your SortedStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(val);
* obj.Pop();
* param_3 := obj.Peek();
* param_4 := obj.IsEmpty();
*/