解題思路:利用stack 重寫 MinStack()
class MinStack {
public:
/** initialize your data structure here. */
stack<int>res;
stack<int>min;
void push(int x) {
res.push(x);
if(min.empty()||x<=min.top())
min.push(x);
}
void pop() {
if (!min.empty()) {
if (res.top() == min.top())
min.pop();
res.pop();
}
}
int top() {
return res.top();
}
int getMin() {
return min.top();
}
};