栈的最小值

来源:互联网 发布:易语言捕鱼游戏源码 编辑:程序博客网 时间:2024/06/06 01:19

今天刷题看到了一个求栈最小值的方法,觉得很不错啊~~


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;            }};//非原创,转自牛客网



0 0
原创粉丝点击