https://leetcode.cn/problems/implement-queue-using-stacks/description/
<aside> 💡
思路:使用一个进栈和一个出栈模拟队列
class MyQueue {
public:
MyQueue() {}
void push(int x) {
while (!stackOut.empty()) {
stackIn.push(stackOut.top());
stackOut.pop();
}
stackIn.push(x);
while (!stackIn.empty()) {
stackOut.push(stackIn.top());
stackIn.pop();
}
}
int pop() {
int tmp = stackOut.top();
stackOut.pop();
return tmp;
}
int peek() {
return stackOut.top();
}
bool empty() {
return stackOut.empty();
}
private:
stack<int> stackIn;
stack<int> stackOut;
};