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问题

    #import "EXFifthViewController.h" @interface EXFifthViewController () @end @implementation ...

  2. 【译】OpenStack Heat基础介绍

    原文:http://blog.scottlowe.org/2014/05/01/an-introduction-to-openstack-heat/ 本文将简要地介绍OpenStack Heat. H ...

  3. PMP项目管理学习笔记(11)——范围管理之定义范围

    定义范围过程组 定义范围包含将项目分解为团队成员要完成的具体工作之前你需要知道的所有一切. 输入:需求文档.项目章程.组织过程资产 工具:辅助工作室.产品分析.代理方案识别.专家判断 辅助工作室: 与 ...

  4. innerHTML与IE浏览器内存泄露问题

    使用 sIEve 扫描和筛选 如果大量使用 JavaScript 和 Ajax 技术开发 Web 2.0 应用程序,您很有可能会遇到浏览器的内存泄漏问题.如果您有一个单页应用程序或者一个页面要处理很多 ...

  5. react基础语法(一)元素渲染和基础语法规则

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. uva1612 Guess

    和cf的打分有点像啊 因为一共只有三道题,所以每个人的得分最多有8种可能性.把这8种可能性都算出来,存在数组里,排好序备用排名就是一个天然的链表,给出了扫描的顺序扫描时,维护两个变量:前一个playe ...

  7. PHP07 函数

    学习要点 函数的定义 自定义函数 函数的工作原理和结构化编程 PHP变量范围 声明及应用各种形式的PHP函数 递归函数 使用自定义函数库 匿名函数和闭包 常用PHP系统函数 PHP7函数新特性 函数的 ...

  8. Java会话(session)管理

    会话概述 什么是会话 简单的理解:用户打开浏览器,点击多个超链接,访问Web服务器上多个资源,然后关闭浏览器,整个过程称之为一次会话. 需要解决的问题 每个用户在使用浏览器与服务器会话的过程中,会产生 ...

  9. eclipse下svn的分支与合并指南 - 更新版

    http://wenku.baidu.com/link?url=ul5vzBHZpHgzENp46RQwTYrkCUYLeVg9TuhmPM_qisR1BGzp6Qca7onhS-SOzwDYuYdA ...

  10. luogu P1462 通往奥格瑞玛的道路--spfa+二分答案

    P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...