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. 【HEVC帧间预测论文】P1.9 Coding Tree Depth Estimation for Complexity Reduction of HEVC

    Coding Tree Depth Estimation for Complexity Reduction of HEVC <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见: ...

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

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

  3. Android(java)学习笔记166:上下文的区分

    1.两种上下文:  (1)Activity.this                               界面的上下文 (2)getApplicationContext()         整 ...

  4. Python 基础语法学习(第一讲)---类的使用

    [写在前面]:其实自学python有一段时间了,但是一直没想起来要写博客来记录自己的学习,今天才感觉要写点什么让自己学的更扎实一点,所以从今天开始更新python自学系列,希望看见文章的大佬们可以指点 ...

  5. Hadoop集群安装指南(CHD5.9.1)(分布式+图文详解)

    centos7.1,CDH5.9.1,3台机器,终极指导安装 下载链接如下: 安装文件下载链接如下: 链接:https://pan.baidu.com/s/1RQYNiWn9a-T8GXcCsoDBs ...

  6. [LOJ] 分块九题 4

    https://loj.ac/problem/6280 区间修改,区间求和. 本来线段树的活. //Stay foolish,stay hungry,stay young,stay simple #i ...

  7. Ubuntu中python链接本地数据库

    由于python链接数据库需要下载DB API模块:例如你需要访问Mysql数据,你需要MySQL数据库模块. DB-API是一个规范. 以便为不同的底层数据库系统和数据库接口程序提供一致的访问接口. ...

  8. Python:字体颜色

    /033[31;1m     /033[0m 字体背景颜色 40:黑 41:深红 42:绿 43:黄色 44:蓝色 45:紫色 46:深绿 47:白色 字体颜色 30:黑 31:红 32:绿 33:黄 ...

  9. JavaScript基础对象---Map

    一.创建Map对象 Map 对象保存键值对.任何值(对象或者原始值) 都可以作为一个键或一个值 1.构造函数 语法:new Map([iterable])参数:         iterable  可 ...

  10. python初体验 ——>>> 模拟体育竞技

    python初体验 ——>>> 模拟体育竞技 一.排球训练营 1. 简介: 模拟不同的两个队伍进行排球的模拟比赛. 2. 模拟原理: 通过输入各自的能力值(Ⅰ),模拟比赛的进行( P ...