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
| // @Title: 用队列实现栈 (Implement Stack using Queues)
// @Author: 15816537946@163.com
// @Date: 2019-11-30 15:48:31
// @Runtime: 0 ms
// @Memory: 1.9 MB
type MyStack struct {
list []int
}
/** Initialize your data structure here. */
func Constructor() MyStack {
return MyStack{list:make([]int,0)}
}
/** Push element x onto stack. */
func (this *MyStack) Push(x int) {
this.list = append(this.list, x)
}
/** Removes the element on top of the stack and returns that element. */
func (this *MyStack) Pop() int {
val := this.Top()
this.list = this.list[:len(this.list)-1]
return val
}
/** Get the top element. */
func (this *MyStack) Top() int {
return this.list[len(this.list)-1]
}
/** Returns whether the stack is empty. */
func (this *MyStack) Empty() bool {
return len(this.list)==0
}
/**
* Your MyStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
* param_3 := obj.Top();
* param_4 := obj.Empty();
*/
|