题目

图片说明

思路

图片说明

Code

class Solution
{
public:
    void push(int node) {        
        stack1.push(node);
    }

    int pop() {
        if(stack2.empty())        //注意 ---只有当栈2为空时才能进行压入操作
        {
            while(!stack1.empty())    //将栈1的元素全部压入栈2
            {
                stack2.push(stack1.top());
                stack1.pop();
            }
        }        
        int node = stack2.top();
        stack2.pop();
        return node;
    }

private:
    stack<int> stack1;
    stack<int> stack2;
};