- push时一直push到主栈
- pop时如果辅助栈为空,将主栈的元素push到辅助栈中,然后pop辅助栈中的元素。
class Solution
{
public:
stack<int> s1;//存储数据
stack<int> s2;
void push(int node) {
s1.push(node);
}
int pop() {
if(s2.empty()){
if(s1.empty()) return -1;
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
}
int t=s2.top();
s2.pop();
return t;
}
private:
stack<int> stack1;
stack<int> stack2;
};