-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode232.cpp
49 lines (45 loc) · 876 Bytes
/
leetcode232.cpp
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
class Queue {
public:
stack<int> in;
stack<int> out;
// Push element x to the back of queue.
void push(int x) {
in.push(x);
}
// Move element x from in to out
void move()
{
while(!in.empty())
{
int x = in.top();
in.pop();
out.push(x);
}
}
// Removes the element from in front of queue.
void pop(void) {
if(out.empty())
{
move();
}
if(!out.empty())
{
out.pop();
}
}
// Get the front element.
int peek(void) {
if(out.empty())
{
move();
}
if(!out.empty())
{
return out.top();
}
}
// Return whether the queue is empty.
bool empty(void) {
return in.empty() && out.empty();
}
};