leetcode 155. 最小栈(开启一个辅助栈 每次push min(辅助栈栈顶,x))
程序员文章站
2022-06-04 16:17:08
...
class MinStack {
public:
stack<int>s;
stack<int>ss;
void push(int x) {s.push(x),ss.push(ss.empty()?x:min(x,ss.top()));}
void pop() {s.pop(),ss.pop();}
int top() {return s.top();}
int getMin() {return ss.top();}
};