https://leetcode.com/problems/min-stack/
class MinStack {
public:
/** initialize your data structure here. */
stack<int> stk, minstk;
MinStack() {
}
int top() {
return stk.top();
}
void push(int x) {
stk.push(x);
if (minstk.empty() || x <= minstk.top()) {
minstk.push(x);
}
}
int pop() {
int top = stk.top();
stk.pop();
if (top == minstk.top()) {
minstk.pop();
}
return top;
}
int getMin() {
return minstk.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
网友评论