352. 将数据流变为多个不相交区间 pair 下的 lower_bound()

在这里插入图片描述
在这里插入图片描述

可以用multimap或者 multiset去维护有序空间,这里用multiset,里面设置为pair<int,int>, 用来存放内部区间的左右两个端点。加入两个哨兵节点
每次插入的时候使用upper_bound找到第一个大于插入的数的迭代器位置:
auto l = S.upper_bound({x, -1e8});
注意这里S是mutiset的 一个对象,里面排序的规则是先用first排序后在用second排序,也就是说当first相等时,second小的排在前面,这里第二个second用一个最小的数值,正好找到第一个大于等于他的迭代器,–l就是第一个小于等于他的迭代器。
分三种情况讨论, 见代码

注意define 在 class以外, 关于迭代器的first和second,这两种等同:(*l).first <=====> l->first

code

#define a first 
#define b second

class SummaryRanges {
public:
    typedef pair<int,int> PII;
    multiset<PII>S;
    SummaryRanges() {
        S.insert({-1e8, -1e8});
        S.insert({1e8, 1e8});
    }
    
    void addNum(int x) {
        auto l = S.upper_bound({x, -1e8});
        auto r = l;
        --l;
        if ((*l).b + 1 == x && x == (*r).a - 1) {
            S.insert({(*l).a, (*r).b});
            S.erase(l);
            S.erase(r); //此时还是指向l和r的迭代器
        }
        else if ((*l).b + 1 == x) {
            S.insert({(*l).a, x});
            S.erase(l);
        }
        else if (x == (*r).a - 1) {
            S.insert({x, (*r).b});
            S.erase(r);
        }
        else if ((*l).b + 1 < x && (*r).a - 1 > x) S.insert({x, x});
    }
    
    vector<vector<int>> getIntervals() {
        vector<vector<int>> res;
        for (auto s : S) {
            if (s.a == -1e8 || s.a == 1e8) continue;
            res.push_back({s.a, s.b});
        }
        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();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值