今天刷题看到了一个求栈最小值的方法,觉得很不错啊~~
class Solution {
public:
stack< pair<int,int> >s;
void push(int value) {
if(s.empty())s.push(pair<int,int>(value,value));
else if(value<min())s.push(pair<int,int>(value,value));
else s.push(pair<int,int>(value,min()));
}
void pop() {
s.pop();
}
int top() {
return s.top().first;
}
int min() {
return s.top().second;
}
};//非原创,转自牛客网