c++ release by map lower_bound


  • 0
    H

    '''
    void addNum(int val) {
    if (m.size()==0) {
    m[val] = Interval(val,val);
    return;
    }

    auto it1 = m.lower_bound(val);
    
    if (it1==m.end()) {
        auto e = m.find(val-1);
        if (e!=m.end()) {
            int end=e->second.end+1;
            int start = e->second.start;
            m.erase(e);
            m[end] = Interval(start, end);
        }
        else m[val] = Interval(val,val);
        return;
    }
    if (it1==m.begin()) {
        if (val == it1->second.start-1) it1->second.start--;
        else if (val < (it1->second.start-1)) m[val] = Interval(val,val);
        return;
    }
    
    
    auto it2 = --it1;
    it1++;
    
    
    //cout<<it1->first<<" : "<<it2->first<<endl;
    if (val == (it1->second.start-1) && val != (it2->second.end+1)){
        it1->second.start--;
        return;
    }
    if (val != (it1->second.start-1) && val == (it2->second.end+1)){
        //it2->second.end++;
        int end = it2->second.end+1;
        int start = it2->second.start;
        m.erase(it2);
        m[end] = Interval(start, end);
        return;
    }
    if (val == (it1->second.start-1) && val == (it2->second.end+1)){
        Interval v(it2->second.start, it1->second.end);
        m.erase(it1);
        m.erase(it2);
        m[it1->second.end] = v;
        return;
    }
    if (val < (it1->second.start-1) && val > (it2->second.end+1)){
        m[val] = Interval(val, val);
        return;
    }
    return;
    
    
    }
    
    vector<Interval> getIntervals() {
    vector<Interval> vec;
    for (auto it=m.begin();it!=m.end();it++) {
        vec.push_back(it->second);
    }
    return vec;
    
    }
    map<int, Interval> m;
    

    '''
    c++ red black tree is map, the api in map is not good


Log in to reply
 

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