#include <stack>
#include <algorithm>
class Solution {
stack<int> st;
stack<int> minst;
public:
void push(int value) {
st.push(value);
if(minst.empty())minst.push(value);
else minst.push(std::min(minst.top(),value));//注意min要std声明一下
}
void pop() {
st.pop();
minst.pop();
}
int top() {
return st.top();
}
int min() {
return minst.top();
}
};
双栈实现最小值,注意一下命名空间。

京公网安备 11010502036488号