-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplement-queue-using-stacks.go
84 lines (68 loc) · 1.26 KB
/
implement-queue-using-stacks.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
package main
// source: https://leetcode.com/problems/implement-queue-using-stacks/
type Stack struct {
values []int
}
func NewStack() Stack {
return Stack{values: make([]int, 0)}
}
func (s *Stack) Push(val int) {
s.values = append(s.values, val)
}
func (s *Stack) Pop() int {
if len(s.values) == 0 {
return -1
}
val := s.values[len(s.values)-1]
s.values = s.values[:len(s.values)-1]
return val
}
func (s *Stack) Len() int {
return len(s.values)
}
func (s *Stack) Peek() int {
if len(s.values) == 0 {
return -1
}
return s.values[len(s.values)-1]
}
//======
type MyQueue struct {
s1 Stack
s2 Stack
}
func Constructor() MyQueue {
return MyQueue{
s1: NewStack(),
s2: NewStack(),
}
}
func (q *MyQueue) Push(x int) {
for q.s2.Len() != 0 {
q.s1.Push(q.s2.Pop())
}
q.s1.Push(x)
}
func (q *MyQueue) Pop() int {
for q.s1.Len() != 0 {
q.s2.Push(q.s1.Pop())
}
return q.s2.Pop()
}
func (q *MyQueue) Peek() int {
for q.s1.Len() != 0 {
q.s2.Push(q.s1.Pop())
}
return q.s2.Peek()
}
func (q *MyQueue) Empty() bool {
return q.s1.Len() == 0 && q.s2.Len() == 0
}
/**
* Your MyQueue object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
* param_3 := obj.Peek();
* param_4 := obj.Empty();
*/