class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop(){
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.top());
stack1.pop();
}
}
int a = stack2.top();
stack2.pop();
return a;
}
private:
stack<int> stack1;//入隊堆棧
stack<int> stack2;//出隊堆棧
};
<分析>:
入隊:將元素進棧A
出隊:判斷棧B是否爲空,如果爲空,則將棧A中所有元素pop,並push進棧B,棧B出棧;
如果不爲空,棧B直接出棧。