class { private: std::function<bool(std::pair<int, int>, std::pair<int, int>)> cmp = [](std::pair<int, int> a, std::pair<int, int> b) { if(a.first == b.first) { return a.second < b.second; } return a.first < b.first; }; std::set<std::pair<int, int>, decltype(cmp)> set = std::set<std::pair<int, int>, decltype(cmp)>(cmp);
public: SummaryRanges() = default;
voidaddNum(int val){ auto it = set.lower_bound({val, val}); int left = val; int right = val; if(it != set.begin() && (--it)->second + 1 < val) { it++; }
while(it != set.end() && val + 1 >= it->first && val - 1 <= it->second) { left = std::min(left, it->first); right = std::max(right, it->second); it = set.erase(it); } set.insert(it, {left, right}); }
std::vector<std::vector<int>> getIntervals(){ auto res = std::vector<std::vector<int>>(); for(auto num : set) { res.push_back({num.first, num.second}); } return res; } };
/** * Your SummaryRanges object will be instantiated and called as such: * SummaryRanges* obj = new SummaryRanges(); * obj->addNum(val); * vector<vector<int>> param_2 = obj->getIntervals(); */
近期评论