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

 /**
* Definition for an interval.
* 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) {
if(a.start == b.start) return a.end < b.end;
return a.start < b.start;
}
class SummaryRanges {
public:
/** Initialize your data structure here. */ vector<Interval> v;
SummaryRanges() {
v.clear();
} void addNum(int val) {
//cout << val << " " << v.size() << endl;
Interval d(val, val);
if(v.size() == ) v.push_back(d);
else {
int t = lower_bound(v.begin(), v.end(), d, cmp) - v.begin();
//cout << val << " " << t << endl;
if(t == ) {
if(val == v[].start) return;
if(v[].start - == val) {
v[].start = val;
} else {
v.insert(v.begin() + t, d);
}
} else if(t == v.size()) {
if(v[t - ].end >= val) return;
if(v[t - ].end + == val) {
v[t - ].end = val;
} else {
v.push_back(d);
}
} else {
if(v[t - ].start == val || v[t - ].end >= val || v[t].start == val) return;
if(v[t - ].end + == v[t].start) {
v[t - ].end = v[t].end;
v.erase(v.begin() + t);
} else if(v[t - ].end + == val) {
v[t - ].end = val;
} else if(v[t].start - == val) {
v[t].start = val;
} else {
v.insert(v.begin() + t, d);
}
} }
} vector<Interval> getIntervals() {
return v;
}
}; /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* vector<Interval> param_2 = obj.getIntervals();
*/

[leetcode]352. Data Stream as Disjoint Intervals的更多相关文章

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

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  2. leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...

  3. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  4. 352. Data Stream as Disjoint Intervals

    Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...

  5. 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 ...

  6. 352[LeetCode] Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

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

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  8. Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  9. [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

随机推荐

  1. C++ 过载,重写,隐藏

    1.过载:在一个类中(也就是一个作用域),方法名相同,形参表不同的方法. 2.重写:父类方法使用virtual,子类方法和方法的方法名,形参表,返回类型相同,子类可以不使用virtual,但是建议使用 ...

  2. C#-窗体之间传递参数

    主窗体: ].Value.ToString(); AddRoomTypeFrm AddRoomTypeFrm = new AddRoomTypeFrm(getroomtypeid); AddRoomT ...

  3. 网络防火墙实战-基于pfsense(2)

    安装虚拟机 本博客所有内容是原创,如果转载请注明来源 http://blog.csdn.net/myhaspl/

  4. javascript 递归之阶乘

    阶乘,即5! = 5*4*3*2*1, 先看传统的做法,利用while循环实现: function factorial(num){ var result = num; if(num<0){ re ...

  5. (function(){}).call(window) 严格模式匿名函数的this指向undefined

    上次在群里,看到有人发出 (function(){}).call(window) 这么一段代码,问这有什么意义,匿名函数中的this不是始终都指向window的么,为什么还要call,我当时也很疑惑. ...

  6. 启动android程序和虚拟机时候出现如下错误的解决方法

    启动android程序和虚拟机时候出现如下错误的解决方法. 错误重现: [2011-07-13 16:22:48 - Emulator] invalid command-line parameter: ...

  7. eclipse @override错误

    @Override是JDK5 就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override而JDK6 修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现都可以加上@Over ...

  8. JavaScript,Java,php的区分大小写问题

    JavaScript 对大小写敏感. JavaScript 对大小写是敏感的.JavaScript属于弱类型语言 当编写 JavaScript 语句时,请留意是否关闭大小写切换键. 函数 getEle ...

  9. How-to Dump Keys from Memcache--reference

    Submitted by Lars Windolf on 19. October 2012 - 21:53 http://lzone.de/dump%20memcache%20keys You spe ...

  10. Android View的绘制机制流程深入详解(四)

    本系列文章主要着重深入介绍Android View的绘制机制及流程,第四篇主要介绍Android自定义View及ViewGroup的实现方法和流程. 主要介绍了自绘控件.自定义组合控件.自定义继承控件 ...