-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
83 lines (72 loc) · 1.59 KB
/
main.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Interval {
public:
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
string toString() {
ostringstream s;
s << "[" << this->start << "," << this->end << "] ";
return s.str();
}
int start;
int end;
};
class SummaryRanges {
public:
/** Initialize your data structure here. */
SummaryRanges() {}
void addNum(int val) {
Interval cur(val, val);
vector<Interval> res;
int pos = 0;
for (auto a : v) {
if (cur.end + 1 < a.start) {
res.push_back(a);
} else if (cur.start > a.end + 1) {
res.push_back(a);
++pos;
} else if (cur.start - 1 == a.end) {
cur.start = a.start;
} else if (cur.end + 1 == a.start) {
cur.end = a.end;
} else {
cur.start = min(cur.start, a.start);
cur.end = max(cur.end, a.end);
}
}
res.insert(res.begin() + pos, cur);
v = res;
}
vector<Interval> getIntervals() {
return v;
}
private:
vector<Interval> v;
};
int main() {
SummaryRanges obj;
vector<int> stream;
vector<Interval> intervals;
stream = {1, 3, 7, 2, 6};
for (int i: stream) {
cout << "add: " << i << endl;
obj.addNum(i);
intervals = obj.getIntervals();
for (Interval interval: intervals) {
cout << interval.toString() << endl;
}
}
return 0;
}