问题:
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
#include <stack>
using namespace std;
class MinStack {
private:
stack<int> content;
stack<int> mins;
public:
void push(int x) {
content.push(x);
//must add =, duplicate elements.
if(mins.empty() || x <= mins.top())
mins.push(x);
}
void pop() {
int top = content.top();
content.pop();
if(top == mins.top())
{
mins.pop();
}
}
int top() {
return content.top();
}
int getMin() {
return mins.top();
}
};