https://leetcode-cn.com/problems/implement-queue-using-stacks/
//stIn完全用来压入数据
//stOut是stIn的反序,用来弹出数据
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
public:
MyQueue() {
}
// 入栈进入stIn
void push(int x) {
stIn.push(x);
}
int pop() {
// 如果Out是空的,从顶部Push stIn的全部数据,以此完成stIn的反序
if (stOut.empty()) {
while (!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
// 从stOut出
int result = stOut.top();
stOut.pop();
return result;
}
//
int peek() {
int res = pop();
stOut.push(res);
return res;
}
bool empty() {
return stIn.empty() && stOut.empty();
}
};
/**
* 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();
*/