352. Data Stream as Disjoint Interval
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的更多相关文章
- [LeetCode] 352. Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- 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 ...
- 【leetcode】352. Data Stream as Disjoint Intervals
问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...
- 352. Data Stream as Disjoint Intervals
Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...
- 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 ...
- [leetcode]352. Data Stream as Disjoint Intervals
数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
- 352[LeetCode] Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- 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 ...
随机推荐
- git push时报错filename too long的解决
命令行输入:git config core.longpaths true 之后再进行 git 的push命令
- 修改JRE system library
MyEclipse 默认的情况下JRE system library 是:MyEclipse 的,如何修改工程中的JRE system library呢?步骤如下: 1.选择工程->Proper ...
- 用JS获取Html中所有图片文件流然后替换原有链接
function displayHtmlWithImageStream(bodyHtml) { var imgReg = /<img.*?(?:>|\/>)/gi; var arr ...
- SEO 第十章
SEO第十章 本次课目标: 1. 站外优化方案计划 2. 常见的SEO作弊手段(黑帽) 3. 百度站长平台的使用 4. 网站流量提升和转化率提升 一.站外优化方案计划 友情链接 权重相当.行业 ...
- leetcode_268.missing number
给定一个数组nums,其中包含0--n中的n个数,找到数组中没有出现的那个数. 解法一:cyclic swapping algorithm class Solution { public: int m ...
- forward reference extends over definition of value
在scala代码中定义了一个方法,,刚开始直接代码中报错,,后来编译是一直报错,最后只是在sc.stop后边加了一个中括号解决,方法体不能放在main主函数中
- vc枚举本机端口信息API
常用的获取端口信息的函数: GetTcpTableGetExtendedTcpTableGetUdpTableGetExtendedUdpTable GetTcp6Table function Get ...
- Java练习demo 20190402 优惠券扣减
实体类: package org.jimmy.autosearch2019.pojo; import java.math.BigDecimal; public class TestEntity2019 ...
- L_01 网络字节顺序
(1)计算机在存储多字节数据时存在大端字节顺序和小端字节顺序两种方式. 大端:高位字节排放在内存的低地址端(即该值的起始地址),低位字节排放在内存的高地址端. 小端:低位字节排放在内存的低地址端(即该 ...
- MySQL数据库文件
MySQL数据库文件 本文档从MySQL数据库和存储引擎层面介绍各种类型的文件. 参数文件(my.cnf) 错误日志(error log) 二进制日志文件(binary log) 慢查询日志(slow ...