题目链接:https://leetcode.com/problems/implement-queue-using-stacks/
class MyQueue {
stack<int> inStack,outStack;
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
inStack.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
in2out();
int x = outStack.top();
outStack.pop();
return x;
}
/** Get the front element. */
int peek() {
in2out();
return outStack.top();
}
void in2out(){
if(outStack.empty()){
while(!inStack.empty()){
int x = inStack.top();
outStack.push(x);
inStack.pop();
}
}
}
/** Returns whether the queue is empty. */
bool empty() {
return inStack.empty() && outStack.empty();
}
};
232. Implement Queue using Stacks
原文:https://www.cnblogs.com/bky-hbq/p/12686230.html