mirror of https://github.com/doocs/leetcode.git
38 lines
638 B
C++
38 lines
638 B
C++
class MinStack {
|
|
public:
|
|
/** initialize your data structure here. */
|
|
MinStack() {
|
|
stk2.push(INT_MAX);
|
|
}
|
|
|
|
void push(int x) {
|
|
stk1.push(x);
|
|
stk2.push(min(x, stk2.top()));
|
|
}
|
|
|
|
void pop() {
|
|
stk1.pop();
|
|
stk2.pop();
|
|
}
|
|
|
|
int top() {
|
|
return stk1.top();
|
|
}
|
|
|
|
int getMin() {
|
|
return stk2.top();
|
|
}
|
|
|
|
private:
|
|
stack<int> stk1;
|
|
stack<int> stk2;
|
|
};
|
|
|
|
/**
|
|
* 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();
|
|
*/ |