思想分析:
实现方案:
class MinStack { public: MinStack() {} void push(int x) { if (StackNum.empty()) { StackNum.push(x); StackMin.push(x); } else { if (x <= StackMin.top()) StackMin.push(x); StackNum.push(x); } } void pop() { if (StackMin.top() == StackNum.top()) StackMin.pop(); StackNum.pop(); } int top() { return StackNum.top(); } int getMin() { return StackMin.top(); } protected: stack<int> StackNum; stack<int> StackMin; };
两个栈实现最小栈
时间: 2024-10-24 19:17:56