-
Notifications
You must be signed in to change notification settings - Fork 0
/
201019-1.cpp
72 lines (67 loc) · 1.58 KB
/
201019-1.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
// https://leetcode-cn.com/problems/data-stream-as-disjoint-intervals/
#include <iostream>
#include <vector>
using namespace std;
class SummaryRanges {
public:
/** Initialize your data structure here. */
SummaryRanges() {
}
void addNum(int val) {
for (int i = 0; i < a.size(); ++i) {
if (val < a[i][0] - 1) {
a.insert(a.begin() + i, vector<int>{val, val});
return;
} else if (val == a[i][0] - 1) {
a[i][0] = val;
return;
} else if (val >= a[i][0] && val <= a[i][1]) {
return;
} else if (val == a[i][1] + 1) {
if (i + 1 >= a.size() || val < a[i + 1][0] - 1) {
a[i][1] = val;
} else {
a[i][1] = a[i + 1][1];
a.erase(a.begin() + i + 1);
}
return;
}
}
a.push_back(vector<int>{val, val});
}
vector<vector<int>> getIntervals() {
return a;
}
private:
vector<vector<int>> a;
};
void print(const vector<vector<int>>& a) {
for (auto& p : a) {
cout << "[" << p[0] << "," << p[1] << "] ";
}
cout << endl;
}
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges* obj = new SummaryRanges();
* obj->addNum(val);
* vector<vector<int>> param_2 = obj->getIntervals();
*/
int main()
{
/*
* 例如,假设数据流中的整数为 1,3,7,2,6,…,每次的总结为:
* [1, 1]
* [1, 1], [3, 3]
* [1, 1], [3, 3], [7, 7]
* [1, 3], [7, 7]
* [1, 3], [6, 7]
*/
SummaryRanges s;
s.addNum(1); print(s.getIntervals());
s.addNum(3); print(s.getIntervals());
s.addNum(7); print(s.getIntervals());
s.addNum(2); print(s.getIntervals());
s.addNum(6); print(s.getIntervals());
return 0;
}