编程语言
首页 > 编程语言> > 46 - 算法 - Leetcode-155-最小栈

46 - 算法 - Leetcode-155-最小栈

作者:互联网

//更新当前的状态 不要漏掉了

class MinStack {
public:
    /** initialize your data structure here. */
    stack<int> temStack;
    int min=INT_MAX;  //当前最小值
    MinStack() {
		
    }
    
    void push(int val) {
        if(val <= min) min = val;
        temStack.push(min);
        temStack.push(val);      
    }
    
    void pop() {
        temStack.pop();
        temStack.pop();
        if(!temStack.empty())
        {
            min = getMin();
        }
        else
        {
            min = INT_MAX;
        }
    }
    
    int top() {
        return temStack.top();
    }
    
    int getMin() {
        int temp = temStack.top();
        temStack.pop();
        int res = temStack.top();
        temStack.push(temp);
        return res;
    }
};

标签:155,min,46,top,temStack,int,pop,getMin,Leetcode
来源: https://blog.csdn.net/tpaixuexi/article/details/115769585