classMyQueue{public:
stack<int> stIn;
stack<int> stOut;/** Initialize your data structure here. */MyQueue(){}/** Push element x to the back of queue. */voidpush(int x){
stIn.push(x);}/** 优化 */intpop(){// 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)if(stOut.empty()){// 从stIn导入数据直到stIn为空while(!stIn.empty()){
stOut.push(stIn.top());
stIn.pop();}}int result = stOut.top();
stOut.pop();return result;}/** Get the front element. */intpeek(){int res =this->pop();// 直接使用已有的pop函数
stOut.push(res);// 因为pop函数弹出了元素res,所以再添加回去return res;}/** Returns whether the queue is empty. */boolempty(){return stIn.empty()&& stOut.empty();}};