首页 > 代码库 > leetcode 155. Min Stack

leetcode 155. Min Stack

题目描述:

 技术分享

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

/**
 * 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