-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path352.cpp
More file actions
executable file
·26 lines (25 loc) · 842 Bytes
/
352.cpp
File metadata and controls
executable file
·26 lines (25 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class SummaryRanges {
public:
SummaryRanges() {}
void addNum(int val) {
vector<int> newInterval{val, val};
int i = 0, overlap = 0, n = intervals.size();
for (; i < n; ++i) {
if (newInterval[1] + 1 < intervals[i][0]) break;
if (newInterval[0] <= intervals[i][1] + 1) {
newInterval[0] = min(newInterval[0], intervals[i][0]);
newInterval[1] = max(newInterval[1], intervals[i][1]);
++overlap;
}
}
if (overlap > 0) {
intervals.erase(intervals.begin() + i - overlap, intervals.begin() + i);
}
intervals.insert(intervals.begin() + i - overlap, newInterval);
}
vector<vector<int>> getIntervals() {
return intervals;
}
private:
vector<vector<int>> intervals;
};