-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path225.用队列实现栈.go
More file actions
51 lines (44 loc) · 880 Bytes
/
225.用队列实现栈.go
File metadata and controls
51 lines (44 loc) · 880 Bytes
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
/*
* @lc app=leetcode.cn id=225 lang=golang
*
* [225] 用队列实现栈
*/
package leetcode
// @lc code=start
type MyStack struct {
queue []int
}
func Constructor() MyStack {
return MyStack{
queue: []int{},
}
}
func (this *MyStack) Push(x int) {
l := len(this.queue)
this.queue = append(this.queue, x)
for i := 0; i < l; i++ {
front := this.queue[0]
this.queue = this.queue[1:]
this.queue = append(this.queue, front)
}
}
func (this *MyStack) Pop() int {
front := this.queue[0]
this.queue = this.queue[1:]
return front
}
func (this *MyStack) Top() int {
return this.queue[0]
}
func (this *MyStack) Empty() bool {
return len(this.queue) == 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