题解 | #包含min函数的栈#

包含min函数的栈

http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49

包含min的栈

思路:每个节点保存当前以及之前进栈的最小值

class Solution {
public:
    stack<pair<int,int>> s;
    void push(int value) {
        if(s.empty())
              s.push({value,value});
        else    
            s.push({value,::min(value,s.top().second)});//思路:每个节点保存当前以及之前进栈的最小值
    }
    void pop() {
        s.pop();
    }
    int top() {
        return s.top().first;
    }
    int min() {
        return s.top().second;
    }
};
全部评论

相关推荐

05-11 20:45
已编辑
西南石油大学 Java
程序员小白条:中小厂可以,但主要28届的话,一般都得大三上了,暑假的话机会比较少,全国投或许可以
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务