Data Stream as Disjoint Intervals LeetCode solution in C++

1
class SummaryRanges {
2
public:
3
SummaryRanges() {}
4

5
void addNum(int val) {
6
data_given.insert(val);
7
}
8

9
vector<vector<int>> getIntervals() {
10
vector<vector<int>> res;
11
int start = -1;
12
int end = -1;
13
for (auto &item : data_given) {
14
if (start == -1) {
15
start = item;
16
end = item;
17
} else if (item == end + 1) {
18
end = item;
19
} else {
20
res.push_back({start, end});
21
start = end = item;
22
}
23
}
24

25
if (start != -1) res.push_back({start, end});
26

27
return res;
28
}
29

30
private:
31
set<int> data_given;
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0