一、题目
二、思路
三、代码
class MyQueue {
public:
stack<int>s;
stack<int>t;
MyQueue() {
}
void push(int x) {
while(!t.empty()){
s.push(t.top());
t.pop();
}
s.push(x);
}
int pop() {
while(!s.empty()){
t.push(s.top());
s.pop();
}
int x=t.top();
t.pop();
return x;
}
//返回首个元素
int peek() {
if(t.empty()){
while(!s.empty()){
t.push(s.top());
s.pop();
}
}
return t.top();
}
bool empty() {
if(s.empty() && t.empty()){
return true;
}
return false;
}
};
/**
* 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();
*/