# [leetcode] 352. Data Stream as Disjoint Intervals 解题报告

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals.

For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be:

[1, 1]
[1, 1], [3, 3]
[1, 1], [3, 3], [7, 7]
[1, 3], [7, 7]
[1, 3], [6, 7]


What if there are lots of merges and the number of disjoint intervals are small compared to the data stream's size?

/**
* Definition for an interval.
* struct Interval {
*     int start;
*     int end;
*     Interval() : start(0), end(0) {}
*     Interval(int s, int e) : start(s), end(e) {}
* };
*/
class SummaryRanges {
public:
auto Cmp = [](Interval a, Interval b) { return a.start < b.start; };
auto it = lower_bound(vec.begin(), vec.end(), Interval(val, val), Cmp);
int start = val, end = val;
if(it != vec.begin() && (it-1)->end+1 >= val) it--;
while(it != vec.end() && val+1 >= it->start && val-1 <= it->end)
{
start = min(start, it->start);
end = max(end, it->end);
it = vec.erase(it);
}
vec.insert(it,Interval(start, end));
}

vector<Interval> getIntervals() {
return vec;
}
private:
vector<Interval> vec;
};

/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* vector<Interval> param_2 = obj.getIntervals();
*/

class SummaryRanges {
public:
/** Initialize your data structure here. */
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;
};

• 本文已收录于以下专栏：

## leecode 解题总结：352. Data Stream as Disjoint Intervals

#include #include #include #include #include #include using namespace std; /* 问题: Given a data...
• qingyuanluofeng
• 2017年03月02日 11:15
• 211

## 【leetcode】Data Stream as Disjoint Intervals

• yeqiuzs
• 2016年06月01日 21:47
• 1365

## Data Stream as Disjoint Intervals

• popvip44
• 2016年11月13日 10:46
• 82

## [leetcode] 352. Data Stream as Disjoint Intervals 解题报告

• qq508618087
• 2016年06月01日 07:24
• 4664

## [Leetcode] 352. Data Stream as Disjoint Intervals 解题报告

• magicbean2
• 2017年08月12日 20:39
• 110

## 352. Data Stream as Disjoint Intervals

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen...
• wdlsjdl2
• 2016年07月14日 22:40
• 259

## [LeetCode]352. Data Stream as Disjoint Intervals

https://leetcode.com/problems/data-stream-as-disjoint-intervals/?tab=Description 合并数据流中当前插入的数 ...
• gqk289
• 2017年03月06日 15:07
• 154

## [leetcode-352]Data Stream as Disjoint Intervals(java)

• zdavb
• 2016年07月05日 12:42
• 651

## leetcode 352. Data Stream as Disjoint Intervals

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers see...
• huanghanqian
• 2017年08月08日 18:21
• 111

## [leetcode]352. Data Stream as Disjoint Intervals

• xiaocong1990
• 2017年08月11日 20:04
• 58

举报原因： 您举报文章：[leetcode] 352. Data Stream as Disjoint Intervals 解题报告 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)