-
Notifications
You must be signed in to change notification settings - Fork 0
/
200510-1.cpp
67 lines (61 loc) · 1.22 KB
/
200510-1.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// https://leetcode-cn.com/problems/implement-queue-using-stacks/
#include <iostream>
#include <vector>
using namespace std;
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
a.push_back(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if (b.empty()) {
while (!a.empty()) {
int x = a.back();
a.pop_back();
b.push_back(x);
}
}
int x = b.back();
b.pop_back();
return x;
}
/** Get the front element. */
int peek() {
if (b.empty()) {
while (!a.empty()) {
int x = a.back();
a.pop_back();
b.push_back(x);
}
}
return b.back();
}
/** Returns whether the queue is empty. */
bool empty() {
return a.empty() && b.empty();
}
private:
vector<int> a, b;
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
int main()
{
MyQueue q;
q.push(4);
int p2 = q.pop(); cout << p2 << endl;
int p3 = q.peek(); cout << p3 << endl;
bool p4 = q.empty(); cout << p4 << endl;
return 0;
}