class MinStack {
public:
/** initialize your data structure here. */
stack<int> x_stack;
stack<int> min_stack;
MinStack() {
min_stack.push(INT_MAX);
}
void push(int x) {
x_stack.push(x);
min_stack.push(x < min_stack.top()?x:min_stack.top());
}
void pop() {
x_stack.pop();
min_stack.pop();
}
int top() {
return x_stack.top();
}
int min() {
return min_stack.top();
}
};
LeetCode refers to Offer 30. Stack containing min function
Guess you like
Origin blog.csdn.net/qq_32862515/article/details/109217942
Recommended
Ranking