数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有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. Swift基本语法以及与OC的比较

    一.注释: 1.单行注释和OC一致. 2.块注释中有与OC不同点:可以嵌套注释 二.常量和变量: 1.常量:初始化后可读不可写 let 2.变量:初始化后可读可写 var //不需要指定类型,系统会自 ...

  2. ABAP ALV 颜色设置(行,列,单元格)

    BCALV_EDIT_03 http://blog.sina.com.cn/s/blog_a87b19300102who3.html 关于ALV表格颜色,这种需求在项目中会经常用到. 列颜色 列的颜色 ...

  3. 【转】python中常用第三方包os sys

    1. os 这个模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Windows下 ...

  4. hdu 5465 Clarke and puzzle 二维线段树

    Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  5. Android Touch事件原理加实例分析

    Android中有各种各样的事件,以响应用户的操作.这些事件可以分为按键事件和触屏事件.而Touch事件是触屏事件的基础事件,在进行Android开发时经常会用到,所以非常有必要深入理解它的原理机制. ...

  6. [Practical Git] Show who changed a line last with git blame

    When working on a file, we often want to know who made certain changes last; we can use git blame to ...

  7. 设计模式 - 命令模式(command pattern) 多命令 具体解释

    命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...

  8. InSAR在地面沉降监测中的应用及发展前景

    合成孔径雷达(Synthetic Aperture Radar,SAR)的概念始于20世纪50年代,是正在发展中的极具潜力的微波遥感技术.SAR具有全天时.全天候的工作能力,能够穿透云层,对某些地物具 ...

  9. (转)如何在JavaScript与ActiveX之间传递数据2

    本文研究如何在JS等脚本语言与ActiveX控件之间通信,如何传递各种类型的参数,以及COM的IDispatch接口.使用类似的方法,可以推广到其他所有脚本型语言,如LUA,AutoCad等.本文将研 ...

  10. nginx rewrite 参数和例子

    http://www.cnblogs.com/analyzer/articles/1377684.html ] 本位转自:http://blog.c1gstudio.com/archives/434 ...