描述
实现栈
解决
两个栈来模拟
class MinStack {
public:
stack<int> s1, s2;
MinStack() {
}
void push(int x) {
s1.push(x);
if (s2.empty() || x <= s2.top())
s2.push(x);
}
void pop() {
int temp = s1.top();
s1.pop();
if (temp == s2.top())
s2.pop();
}
int top() {
return s1.top();
}
int getMin() {
return s2.top();
}
};