class MinStack {
public:
/** initialize your data structure here. */
stack<int> stackValue;
stack<int> stackMin;
MinStack() {
}
void push(int x) {
stackValue.push(x);
if (stackMin.empty() || stackMin.top() >= x)
stackMin.push(x);
}
void pop() {
if (stackMin.top() == stackValue.top()) stackMin.pop();
stackValue.pop();
}
int top() {
return stackValue.top();
}
int getMin() {
return stackMin.top();
}
};
本文介绍了如何在C++中设计一个名为`MinStack`的自定义数据结构,它同时存储栈中的元素和其最小值,提供push、pop、top和getMin方法以进行操作。

415

被折叠的 条评论
为什么被折叠?



