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]

Follow up:
What if there are lots of merges and the number of disjoint intervals are small compared to the data stream's size?

本题考查TreeMap,这里区别一下。TreeMap可以检查某一个key值和它相临的比它大一点和比它小一点的key值,还可以用containsKey来检查是否包含该元素。检查的方法分别是lowerKey和higherKey。而TreeSet里面有ceiling和flooring两种方法,分别表示大于等于它和小于等于它的值。本题的key值为Interval里面的start值,value值为Interval。然后进行详细的判断即可。

/**

* Definition for an interval.

* public class Interval {

*     int start;

*     int end;

*     Interval() { start = 0; end = 0; }

*     Interval(int s, int e) { start = s; end = e; }

* }

*/

public class SummaryRanges {

TreeMap<Integer,Interval> map;

/** Initialize your data structure here. */

public SummaryRanges() {

map = new TreeMap<Integer,Interval>();

}

public void addNum(int val) {

if(map.containsKey(val)) return;

Integer lo = map.lowerKey(val);

Integer hi = map.higherKey(val);

if(lo!=null&&hi!=null&&map.get(lo).end+1==val&&hi==val+1){

map.get(lo).end = map.get(hi).end;

map.remove(hi);

}else if(lo!=null&&map.get(lo).end+1>=val){

map.get(lo).end = Math.max(val,map.get(lo).end);

}else if(hi!=null&&hi==val+1){

map.put(val,new Interval(val,map.get(hi).end));

map.remove(hi);

}else{

map.put(val,new Interval(val,val));

}

}

public List<Interval> getIntervals() {

return new ArrayList<Interval>(map.values());

}

}

/**

* Your SummaryRanges object will be instantiated and called as such:

* SummaryRanges obj = new SummaryRanges();

* obj.addNum(val);

* List<Interval> param_2 = obj.getIntervals();

*/

352. Data Stream as Disjoint Interval的更多相关文章

  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. [leetcode]352. Data Stream as Disjoint Intervals

    数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...

  7. 352[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 分离区间的数据流

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

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

随机推荐

  1. IOS之pageControl

    用户点击页面控件,会触发UIControlEventValueChanged事件,并启动设置为控件动作的任何方法.可以通过调用currentPage查询控件的新值,并通过调整numberOfPages ...

  2. 开启apahce的mod_speling.so模块,让使用apahce http服务器不再有大小写烦恼

    今天把服务器重新安装系统,做apache调优前,优化下apache对网络地址大小写不区分的支持.记录如下: 编译mod_speling.so模块去除Apache-url大小写字母敏感的配置 1. 进入 ...

  3. 【转载】Hierarchal Temporal Memory (HTM)

    最近在看机器学习,看能否根据已有的历史来预测Hardware的故障发生概率.下文是一篇很有意思的文章,转自 http://numenta.org/htm.html. NuPIC是一个开源项目,用来实现 ...

  4. Netbeans Makefile: recipe for target 'XXX' failed 运行failed(退出值 -1073741511 找不到C/C++库文件,关键字

      今天不知怎么的又出错了 吐血了 找不到NULL new等关键字了   看到知乎上有人和我一个问题,怎么办?很简单卸载了以前的cygwin和netbeans然后重装,我重装时没有把以前的cygwin ...

  5. 如何理解Python中的if __name__ == '__main__'

    1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__name__ == '小明'):在你自己眼中,你是你自己(__name__ == '_ ...

  6. 关于Ubuntu 16.04中E: Could not get lock /var/lib/dpkg/lock - open的三种解决方案

    问题 在Ubuntu中,有时候运用sudo  apt-get install 安装软件时,会出现如下的情况: E: Could not get lock /var/lib/dpkg/lock - op ...

  7. numpy次方计算

    >>> 2**np.arange(3, 6) array([ 8, 16, 32])

  8. Linux-03 Linux下的tar命令

    功能说明 用来建立,还原备份文件的工具程序,它可以加入,解开备份文件内的文件 参数 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五 ...

  9. 通过java反射机制,获取对象的属性和值(包括所有继承的父类)

    java的反射机制提供了两种方法: getDeclaredFields() :该方法能获取到本类的所有属性,包括private,protected和public,但不能获取到继承的父类的属性. get ...

  10. merge dict key

    #!/usr/local/python # -*- coding:utf-8 -*-user_dict = {'python': 23, 'Python': 51, '机器':10, 'PYTHON' ...