码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 155. Min Stack

时间:2018-07-14 16:33:17      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:bsp   new   cal   style   ted   str   get   pre   ram   

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

标签:bsp   new   cal   style   ted   str   get   pre   ram   

原文地址:https://www.cnblogs.com/randyniu/p/9309534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!