class MinStack {
public:
void push(int x) {
sta.push(x);
if(MinVal.size() == 0 || x < MinVal.top())
MinVal.push(x);
else
MinVal.push(MinVal.top());
}
void pop() {
sta.pop();
MinVal.pop();
}
int top() {
return sta.top();
}
int getMin() {
return MinVal.top();
}
stack<int> sta;
stack<int> MinVal;
};
LeetCode || Min Stack
最新推荐文章于 2024-03-31 19:08:05 发布