包含min函数的栈

来源:互联网 发布:mac 好用的卸载软件 编辑:程序博客网 时间:2024/06/07 07:01
#include <stack>using namespace std;class Solution {public:    void push(int value) {        s.push(value);    }    void pop() {        if (!s.empty())            {s.pop();}    }    int top() {        return s.top();    }    int min() {        stack<int> temp;        int max = ~(1 << (sizeof(int) * 8 - 1));        while (!s.empty()) {            temp.push(top());            if (top() < max) {                max = top();            }            pop();        }        while (!temp.empty())        {            s.push(temp.top());            temp.pop();        }        return max;    }private:    stack<int> s;};