leetcode/solution/0200-0299/0232.Implement Queue using .../Solution.cpp

47 lines
793 B
C++

class MyQueue {
public:
MyQueue() {
}
void push(int x) {
stk1.push(x);
}
int pop() {
move();
int ans = stk2.top();
stk2.pop();
return ans;
}
int peek() {
move();
return stk2.top();
}
bool empty() {
return stk1.empty() && stk2.empty();
}
private:
stack<int> stk1;
stack<int> stk2;
void move() {
if (stk2.empty()) {
while (!stk1.empty()) {
stk2.push(stk1.top());
stk1.pop();
}
}
}
};
/**
* 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();
*/