今天刷題看到了一個求棧最小值的方法,覺得很不錯啊~~
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;
}
};//非原創,轉自牛客網