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

包含min函数的栈

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

#include <climits>
class Solution {
private:
    stack<int> sta1;
    stack<int> sta2;
public:
    void push(int value) {
        sta1.push(value);
        if(sta2.empty()||sta2.top()>value){
            sta2.push(value);
        }else{
            sta2.push(sta2.top());
        }

            
    }
    void pop() {
        sta1.pop();
        sta2.pop();
    }
    int top() {
        return sta1.top();
    }
    int min() {
        return sta2.top();
    }
};

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务