Description:
题目大意:用两个栈实现队列。
解题思路:
算法标签:栈
代码:
class Solution
{
public:
void push(int node) {
stack1.push(node);
return;
}
int pop() {
while(!stack2.empty())
stack2.pop();
while(!stack1.empty()) {
stack2.push(stack1.top());
stack1.pop();
}
int ans = stack2.top();
stack2.pop();
while(!stack2.empty()) {
stack1.push(stack2.top());
stack2.pop();
}
return ans;
}
private:
stack<int> stack1;
stack<int> stack2;
};