Leetcode 155. Min Stack

Posted randyniu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 155. Min Stack相关的知识,希望对你有一定的参考价值。

class MinStack {
public:
    /** initialize your data structure here. */
    MinStack() {
        
    }
    
    void push(int x) {
        if(_data.empty())
        {
            _min.push(x);
        }
        else
        {
            if(x < _min.top())
                _min.push(x);
            else
                _min.push(_min.top());
        }
        _data.push(x);  
    }
    
    void pop() {
        _data.pop();
        _min.pop();
    }
    
    int top() {
        return _data.top();
    }
    
    int getMin() {
        return _min.top();
    }
private:
    std::stack<int> _data;
    std::stack<int> _min;
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

 

以上是关于Leetcode 155. Min Stack的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 155. Min Stack

Leetcode 155. Min Stack

LeetCode 155 Min Stack

leetcode155. Min Stack

leetcode-155-Min Stack

[LeetCode] 155. Min Stack