解题思路:用两个栈实现队列,其中一个入栈,一个出栈。
源代码:
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
stk1.push(x);
}
int pop() {
if (stk2.empty()) { //如果stk2是空的,那么把stk1都注入stk2中
while (!stk1.empty()) {
stk2.push(stk1.top());
stk1.pop();
}
}
int ans = stk2.top();
stk2.pop();
return ans;
}
int peek() {
if (stk2.empty()) { //如果stk2是空的,那么把stk1都注入stk2中
while (!stk1.empty()) {
stk2.push(stk1.top());
stk1.pop();
}
}
return stk2.top();
}
bool empty() {
if (stk1.empty() && stk2.empty()) return true;
else return false;
}
private:
stack<int> stk1;
stack<int> stk2;
};
/**
* 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();
*/
运行结果: