题目描述:
首刷自解:
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
s1.push(x);
}
int pop() {
int retVal;
if (s2.empty()) {
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
retVal = s2.top();
s2.pop();
return retVal;
}
int peek() {
if (s2.empty()) {
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
return s2.top();
}
bool empty() {
return s1.empty() && s2.empty() ;
}
private:
stack<int> s1, s2;
};