class MyQueue {
public:
MyQueue() {
}
void push(int x) {
stack_in.push(x);
}
int pop() {
if(stack_out.empty()) {
while(!stack_in.empty()) {
stack_out.push(stack_in.top());
stack_in.pop();
}
}
int res = stack_out.top();
stack_out.pop();
return res;
}
int peek() {
int res = this->pop();
stack_out.push(res);
return res;
}
bool empty() {
if(stack_in.empty() && stack_out.empty()) return true;
else return false;
}
private:
stack<int> stack_in;
stack<int> stack_out;
};
/**
* 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();
*/
力扣232. 用栈实现队列
最新推荐文章于 2025-05-22 10:04:48 发布