-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path232.implement-queue-using-stacks.go
117 lines (102 loc) · 2.62 KB
/
232.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* @lc app=leetcode id=232 lang=golang
*
* [232] Implement Queue using Stacks
*
* https://leetcode.com/problems/implement-queue-using-stacks/description/
*
* algorithms
* Easy (41.56%)
* Likes: 524
* Dislikes: 107
* Total Accepted: 147.6K
* Total Submissions: 342.3K
* Testcase Example: '["MyQueue","push","push","peek","pop","empty"]\n[[],[1],[2],[],[],[]]'
*
* Implement the following operations of a queue using stacks.
*
*
* push(x) -- Push element x to the back of queue.
* pop() -- Removes the element from in front of queue.
* peek() -- Get the front element.
* empty() -- Return whether the queue is empty.
*
*
* Example:
*
*
* MyQueue queue = new MyQueue();
*
* queue.push(1);
* queue.push(2);
* queue.peek(); // returns 1
* queue.pop(); // returns 1
* queue.empty(); // returns false
*
* Notes:
*
*
* You must use only standard operations of a stack -- which means only push to
* top, peek/pop from top, size, and is empty operations are valid.
* Depending on your language, stack may not be supported natively. You may
* simulate a stack by using a list or deque (double-ended queue), as long as
* you use only standard operations of a stack.
* You may assume that all operations are valid (for example, no pop or peek
* operations will be called on an empty queue).
*
*
*/
// Reference: http://bit.ly/2FDTuuv
type MyQueue struct {
s1 []int
s2 []int
front int
}
/** Initialize your data structure here. */
func Constructor() MyQueue {
return MyQueue{
s1: []int{},
s2: []int{},
}
}
/** Push element x to the back of queue. */
func (this *MyQueue) Push(x int) {
if len(this.s1) == 0 {
this.front = x
}
this.s1 = append(this.s1, x)
}
/** Removes the element from in front of queue and returns that element. */
func (this *MyQueue) Pop() int {
if len(this.s2) == 0 {
for i := len(this.s1); i > 0; i-- {
this.s2 = append(this.s2, this.s1[i-1])
}
this.s1 = this.s1[:0]
}
pop := this.s2[len(this.s2)-1]
this.s2 = this.s2[:len(this.s2)-1]
if len(this.s2) > 0 {
this.front = this.s2[len(this.s2)-1]
}
return pop
}
/** Get the front element. */
func (this *MyQueue) Peek() int {
return this.front
}
/** Returns whether the queue is empty. */
func (this *MyQueue) Empty() bool {
if len(this.s1) == 0 && len(this.s2) == 0 {
return true
}
return false
}
/**
* 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();
*/