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

相关文章:

  • 2021-12-08
  • 2021-09-29
  • 2021-12-20
  • 2021-10-26
  • 2021-07-09
  • 2021-07-11
  • 2021-05-18
猜你喜欢
  • 2021-07-27
  • 2021-09-23
相关资源
相似解决方案