Share my ugly c++ solution~


  • 0
    Y
    class MinStack {
    public:
    vector<int> v;
    vector<int> min;
        void push(int x) {
            v.push_back(x);
            if(min.empty()) min.push_back(x);
            else if(x <= min[min.size() - 1]) min.push_back(x);
        }
    
        void pop() {
            if(top() == min[min.size() - 1]) min.erase(min.begin() + (min.size() - 1),min.end());
            v.erase(v.begin() + (v.size() - 1),v.end());
        }
    
        int top() {
            return v[v.size() - 1];
        }
    
        int getMin() {
            return min[min.size() - 1];
        }
    };

Log in to reply
 

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