352[LeetCode] Data Stream as Disjoint Intervals

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals.

For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be:

[1, 1]
[1, 1], [3, 3]
[1, 1], [3, 3], [7, 7]
[1, 3], [7, 7]
[1, 3], [6, 7]


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>

using namespace std;

struct Interval {
    int start;
    int end;
    Interval() : start(0), end(0) {}
    Interval(int s, int e) : start(s), end(e) {}
};
bool Cmp(Interval a, Interval b) { return a.start < b.start; }//不能将其放在class SummaryRange中
class SummaryRanges {
public:
    void addNum(int val) {

        vector<Interval>::iterator it = lower_bound(vec.begin(), vec.end(), Interval(val, val), Cmp);
        int start = val, end = val;
        if(it != vec.begin() && (it-1)->end+1 >= val) it--;
        while(it != vec.end() && val+1 >= it->start && val-1 <= it->end)
        {
            start = min(start, it->start);
            end = max(end, it->end);
            it = vec.erase(it);
        }
        vec.insert(it,Interval(start, end));
    }

    vector<Interval> getIntervals() {
        return vec;
    }
private:
    vector<Interval> vec;
};

/**
 * Your SummaryRanges object will be instantiated and called as such:
 * SummaryRanges* obj = new SummaryRanges();
 * obj->addNum(val);
 * vector<Interval> param_2 = obj->getIntervals();
 */

 int main(){
     SummaryRanges* obj =new SummaryRanges();
     obj->addNum(1);
     obj->addNum(2);
    obj->addNum(5);
     obj->addNum(6);
     obj->addNum(3);
     vector<Interval> param_2 = obj->getIntervals();
     cout<<param_2[0].start<<"------"<<param_2[0].end<<endl;
     cout<<param_2[1].start<<"------"<<param_2[1].end<<endl;
 }



易错点:

vector<Interval>::iterator it = lower_bound(vec.begin(), vec.end(), Interval(val, val), Cmp);lower_bound()返回的是从begin到end之间的按照Cmp排序的首个大于等于Interval(val,val)的地址;因此it 声明应该是  vector<Interval>::iterator it之前错误的声明为  Interval* it  则出现报错:[Error] cannot convert ‘__gnu_cxx::__normal_iterator<Interval*, std::vector<Interval> >‘ to ‘Interval*‘ in initialization



原文地址:https://www.cnblogs.com/250101249-sxy/p/10424840.html

时间: 2024-11-06 17:21:31

352[LeetCode] Data Stream as Disjoint Intervals的相关文章

[LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

Leetcode: Data Stream as Disjoint Intervals

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

[email&#160;protected] [352] Data Stream as Disjoint Intervals (Binary Search &amp; TreeSet)

https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the da

352. Data Stream as Disjoint Intervals

问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. 解题思路: 这道题是目前最新的题,其实思路很容易找到,难点在于考虑到所有的可能的情形. 首先要确定类必须有一个保存当前结果的集合类List<Interval>,其元素的按Interval的起始值的大小排序,

352. Data Stream as Disjoint Intervals (TreeMap, lambda, heapq)

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

[leetcode]352. Data Stream as Disjoint Intervals

数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样提交了,发现错了,想到之前考虑要不要判重,我感觉是这个问题,然后就是在二分查找的时候,判断一下左右区间是否包含当前的值,包含就直接返回. 1 /** 2 * Definition for an interval. 3 * struct Interval { 4 * int start; 5 * in

352. Data Stream as Disjoint Interval

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

leetcode 352 &amp; leetcode 239 &amp; leetcode 295 &amp; leetcode 53 &amp; leetcode 209

lc352 Data Stream as Disjoint Intervals 可以用treemap解 key保存interval的start,value保存interval的end.分别找出当前val的lowerKey(treemap中>val的最小key值,没有就返回null)和higherKey(<val的最大key没有就返回null) 有以下几种情况: 1) 当前插入的key与前后两个interval重叠 合并三者,将前一个的interval.end改成后一个的end,并且将后一个从t

[LeetCode] Find Median from Data Stream 找出数据流的中位数

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 Design a d