-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path232+MyQueue.cpp
More file actions
47 lines (38 loc) · 1.01 KB
/
232+MyQueue.cpp
File metadata and controls
47 lines (38 loc) · 1.01 KB
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
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
mStT = stack<int>();
while (mStO.size()) { // 把 mStO 里的内容翻转到 mStT 中
int num = mStO.top(); mStO.pop();
mStT.push(num);
}
mStO.push(x); // x 入栈底
while (mStT.size()) { // 再翻转回来
int num = mStT.top(); mStT.pop();
mStO.push(num);
}
}
int pop() {
int num = mStO.top(); mStO.pop();
return num;
}
int peek() {
return mStO.top();
}
bool empty() {
return mStO.empty();
}
private:
stack<int> mStO;
stack<int> mStT;
};
/**
* 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();
*/