forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplement-stack-using-queues.go
executable file
·85 lines (70 loc) · 1.52 KB
/
implement-stack-using-queues.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package problem0225
// MyStack 是用 Queue 实现的 栈
type MyStack struct {
a, b *Queue
}
// Constructor Initialize your data structure here.
func Constructor() MyStack {
return MyStack{a: NewQueue(), b: NewQueue()}
}
// Push Push element x onto stack.
func (ms *MyStack) Push(x int) {
if ms.a.Len() == 0 {
ms.a, ms.b = ms.b, ms.a
}
ms.a.Push(x)
}
// Pop Removes the element on top of the stack and returns that element.
func (ms *MyStack) Pop() int {
if ms.a.Len() == 0 {
ms.a, ms.b = ms.b, ms.a
}
for ms.a.Len() > 1 {
ms.b.Push(ms.a.Pop())
}
return ms.a.Pop()
}
// Top Get the top element.
func (ms *MyStack) Top() int {
res := ms.Pop()
ms.Push(res)
return res
}
// Empty Returns whether the stack is empty.
func (ms *MyStack) Empty() bool {
return (ms.a.Len() + ms.b.Len()) == 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();
*/
// Queue 是用于存放 int 的队列
type Queue struct {
nums []int
}
// NewQueue 返回 *kit.Queue
func NewQueue() *Queue {
return &Queue{nums: []int{}}
}
// Push 把 n 放入队列
func (q *Queue) Push(n int) {
q.nums = append(q.nums, n)
}
// Pop 从 q 中取出最先进入队列的值
func (q *Queue) Pop() int {
res := q.nums[0]
q.nums = q.nums[1:]
return res
}
// Len 返回 q 的长度
func (q *Queue) Len() int {
return len(q.nums)
}
// IsEmpty 反馈 q 是否为空
func (q *Queue) IsEmpty() bool {
return q.Len() == 0
}