c++题解 用两个栈实现队列
用两个栈实现队列
http://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
用双栈实现队列
class Solution
{
public:
void push(int node) {
s1.push(node);
}
int pop() {
if(s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
}
if(s2.empty())
return -1;
int t = s2.top();
s2.pop();
return t;
}
private:
stack<int> s1;
stack<int> s2;
};