class MyQueue {
public:
/** Initialize your data structure here. */
stack<int> pstack;
stack<int> auxstack;
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
pstack.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int ans = peek();
auxstack.pop();
return ans;
}
/** Get the front element. */
int peek() {
if(auxstack.empty()){
while(!pstack.empty()){
auxstack.push(pstack.top());
pstack.pop();
}
}
return auxstack.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return pstack.empty()&&auxstack.empty();
}
};
leetcode 232. Implement Queue using Stacks
最新推荐文章于 2022-08-14 12:35:34 发布