一个正常的栈,一个最小栈栈顶是当前栈的最小元素
push的时候 如果push的x比最小栈顶的小就push x 否则吧栈顶再push一遍

#include <iostream>
#include <stack>
using namespace std;

class MinStack {
public:
/** initialize your data structure here. */
stack<int>st;
stack<int>mst;
MinStack() {
;
}

void push(int x) {
st.push(x);
if(mst.empty()){
mst.push(x);
}else{
if(x<mst.top()){
mst.push(x);
}else{
mst.push(mst.top());
}
}

}

void pop() {
if(!st.empty()){
st.pop();
mst.pop();
}

}

int top() {
if(!st.empty()){
return st.top();
}else{
return -1;
}
}

int getMin() {
if(!mst.empty()){
return mst.top();
}else{
return -1;
}
}
};

int main()
{
cout << "Hello world!" << endl;
return 0;
}

leetcode 155. 最小栈 单调栈_ios