Leetcode155(力扣155):最小栈

Leetcode155(力扣155):最小栈_第1张图片

class MinStack {
public:
    stack<int> s1;
    stack<int> s2;
    MinStack() {

    }
    
    void push(int x) {
        s1.push(x);
        if(s2.empty()) s2.push(x);
        else s2.push(min(s2.top(),x));
    }
    
    void pop() {
        s1.pop();
        s2.pop();
    }
    
    int top() {
        return s1.top();
    }
    
    int getMin() {
        return s2.top();
    }
};

你可能感兴趣的:(stack,栈,leetcode,算法)