class MinStack {
public:
/** initialize your data structure here. */
stack<int>x_stack,min_stack;
MinStack() {min_stack.push(INT_MAX);}
void push(int x) {
x_stack.push(x);
if(x<=min_stack.top()) min_stack.push(x);
}
void pop() {
if(x_stack.top()==min_stack.top()) {
min_stack.pop();
}
x_stack.pop();
}
int top() {
return x_stack.top();
}
int min() {
return min_stack.top();
}
};
剑指 Offer 30. 包含min函数的栈 Easy
最新推荐文章于 2024-07-22 16:27:42 发布