232. 用栈实现队列
使用两个栈,一个作为输入栈,一个作为输出栈
class MyQueue {
public:
stack<int> in;
stack<int> out;
MyQueue() {
}
void push(int x) {
in.push(x);
}
int pop() {
if(out.empty())
{
while(!in.empty())
{
out.push(in.top());
in.pop();
}
}
int res = out.top();
out.pop();
return res;
}
int peek() {
int res = this->pop();
out.push(res);
return res;
}
bool empty() {
return in.empty()&&out.empty();
}
};