LandscapeMi

landscapemi的博客

leetcode:Data Stream as Disjoint Intervals(352)

https://leetcode.com/problems/data-stream-as-disjoint-intervals/


class SummaryRanges {  
public:  
    /** Initialize your data structure here. */  
    void addNum(int val) {  
        auto it = st.lower_bound(Interval(val, val));  
        int start = val, end = val;  
        if(it != st.begin() && (--it)->end+1 < val) it++;  
        while(it != st.end() && val+1 >= it->start && val-1 <= it->end)  
        {  
            start = min(start, it->start);  
            end = max(end, it->end);  
            it = st.erase(it);  
        }  
        st.insert(it,Interval(start, end));  
    }  

    vector<Interval> getIntervals() {  
        vector<Interval> result;  
        for(auto val: st) result.push_back(val);  
        return result;  
    }  
private:  
    struct Cmp{  
        bool operator()(Interval a, Interval b) { return a.start < b.start; }  
    };  
    set<Interval, Cmp> st;  
};  
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mijian1207mijian/article/details/52385145
个人分类: leetcode(c++)
上一篇leetcode:Binary Search Tree:Contains Duplicate III(220)
下一篇leetcode: Segment Tree:Range Sum Query - Mutable(307)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭