-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path225.用队列实现栈.go
56 lines (42 loc) · 978 Bytes
/
225.用队列实现栈.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
/*
* @lc app=leetcode.cn id=225 lang=golang
*
* [225] 用队列实现栈
*/
// @lc code=start
type MyStack struct {
data []int
}
/** Initialize your data structure here. */
func Constructor() MyStack {
return MyStack{data: []int{}}
}
/** Push element x onto stack. */
func (this *MyStack) Push(x int) {
this.data = append(this.data, x)
return
}
/** Removes the element on top of the stack and returns that element. */
func (this *MyStack) Pop() int {
size := len(this.data)
x := this.data[size]
this.data = this.data[0:size-1]
return
}
/** Get the top element. */
func (this *MyStack) Top() int {
return this.data[len(this.data)-1]
}
/** Returns whether the stack is empty. */
func (this *MyStack) Empty() bool {
return len(this.data) == 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();
*/
// @lc code=end