剑指offer面试题30:包含min函数的栈

    科技2024-10-11  19

    class MinStack { public: stack<int> s1; stack<int> s2; MinStack() { } void push(int x) { s1.push(x); if(s2.empty()) s2.push(x); else if(s2.top()<x) s2.push(s2.top()); else s2.push(x); } void pop() { s1.pop(); s2.pop(); } int top() { return s1.top(); } int min() { return s2.top(); } };
    Processed: 0.028, SQL: 8