mirror of https://github.com/doocs/leetcode.git
47 lines
793 B
C++
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();
|
|
*/ |