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

包含min函数的栈

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

#include <climits>
#include <stack>
class Solution {
public:
    stack<int> st;
    stack<int> st_min;
    void push(int value) {
        
        st.push(value);
        if (st_min.empty() || value <= st_min.top())
            st_min.push(value);

    }
    void pop() {
        int tmp = st.top();
        st.pop();
        if (tmp == st_min.top())
            st_min.pop();
    }

    int top() {
        return st.top();
    }
    int min() {
        return st_min.top();
    }
};

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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