My clear c++ solution beats 89.46%


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.