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. android studio 导入jar包

    或者还可以这么导入: 1.首先先去下载需要的jar包2.将jar包复制到Project下的app–>libs目录下(没有libs目录就新建一个)如下图所示位置: 3.点击工具栏中的Project ...

  2. IOS 根据身份证号码获取 年龄 生日 性别

    /** 从身份证上获取年龄 18位身份证 */ -(NSString *)getIdentityCardAge:(NSString *)numberStr { NSDateFormatter *for ...

  3. SQL Server 查询锁表和接锁表

    SQL Server 查询锁表 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) as tableNa ...

  4. Docker - Image创建

    自己创建Image会有一些好处,可以选择最新的版本,而且从国内的镜像创建时更新软件也会从该镜像获取,速度更快. (1)安装debootstrap zhouh1@uhome:/media/zhouh1/ ...

  5. DHCP server工作原理

    1.CLIENT首先发出广播的DHCPDISCOVER报文,广播的目的是让DHCP SERVER能够收到这个请求报文.在这个报文中,CLIENT可以在"选项"字段中加入" ...

  6. 合并百度影音的离线数据 with python 2.3 格式更新

    很久没有更新了. 这次新增支持四种格式的解析. filelist slicelist download.cfg third_party_download.cfg 还是2个文件.替换之前版本即可. 初步 ...

  7. InvocationTargetException异常的深入研究-servlet的setAttribute与getAttribute

    在某项目中,前端jsp传入的用户id数据通过session域传入后台servlet进行处理的过程中,无意间出现了InvocationTargetException异常 前端部分代码如下:测试代码,非原 ...

  8. 在虚拟机linux环境下编译windows版adb fastboot

    原文出自:http://blog.chinaunix.net/uid-20546441-id-1746200.html 我根据虚拟机编译遇到的问题进行一些添加 [前提条件] Linux Android ...

  9. PHP06 流程控制

    学习要点 选择结构 循环结构 学习目标 掌握PHP的选择结构 掌握PHP的循环结构 流程控制概述 程序 程序:一系列计算机指令的集合. 编程语言:开发程序的工具. 程序执行结构 计算机程序有三种基本执 ...

  10. windows10家庭版 远程桌面报错

    windows10家庭版 远程桌面报错“要求的函数不受支持 ...”,Windows没有编辑组策略选项(gpedit.msc),所以无法按照微软提供的方法来修改组策略.所以我们需要修改注册表的方法来修 ...