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
| class SummaryRanges { map<int, int> m; public: SummaryRanges() {} void addNum(int val) { int left = val, right = val; for (auto it = m.lower_bound(val - 1); it != m.end() && it->second <= right + 1; m.erase(it++)) { int l = it->second, r = it->first; left = min(left, l), right = max(right, r); } m[right] = left; } vector<vector<int>> getIntervals() { vector<vector<int>> v; for (auto it = m.begin(); it != m.end(); ++it) v.push_back({it->second, it->first}); return v; } };
|