352. 将数据流变为多个不相交区间

给定一个非负整数的数据流输入 a1,a2,…,an,…,将到目前为止看到的数字总结为不相交的区间列表。

例如,假设数据流中的整数为 1,3,7,2,6,…,每次的总结为:

[1, 1]

[1, 1], [3, 3]

[1, 1], [3, 3], [7, 7]

[1, 3], [7, 7]

[1, 3], [6, 7]

进阶:

如果有很多合并,并且与数据流的大小相比,不相交区间的数量很小,该怎么办?

提示:

特别感谢 @yunhong 提供了本问题和其测试用例。

class SummaryRanges {

    private List<int[]> list = new ArrayList<>();

    /** Initialize your data structure here. */
public SummaryRanges() {
} public void addNum(int val) {
if (list.size() == 0) {
int[] arr = new int[2];
arr[0] = arr[1] = val;
list.add(arr);
return;
} int insertPosition = findInsertPosition(val);
if (insertPosition == list.size()) {
if (val == list.get(list.size() - 1)[1] + 1) {
list.get(list.size() - 1)[1] = list.get(list.size() - 1)[1] + 1;
} else {
int[] arr = new int[2];
arr[0] = arr[1] = val;
list.add(arr);
}
} else if (insertPosition == 0) {
if (val == list.get(0)[0] - 1) {
list.get(0)[0] = list.get(0)[0] - 1;
} else {
int[] arr = new int[2];
arr[0] = arr[1] = val;
list.add(0, arr);
}
} else if (insertPosition > 0) {
if (val == list.get(insertPosition)[0] - 1 && val == list.get(insertPosition - 1)[1] + 1) {
int index = insertPosition - 1;
int[] front = list.get(index);
int[] behind = list.get(insertPosition); list.remove(index);
list.remove(index); int[] arr = new int[2];
arr[0] = front[0];
arr[1] = behind[1];
list.add(index, arr);
} else if (val == list.get(insertPosition)[0] - 1) {
list.get(insertPosition)[0] = list.get(insertPosition)[0] - 1;
} else if (val == list.get(insertPosition - 1)[1] + 1) {
list.get(insertPosition - 1)[1] = list.get(insertPosition - 1)[1] + 1;
} else {
int[] arr = new int[2];
arr[0] = arr[1] = val;
list.add(insertPosition, arr);
}
}
} public int[][] getIntervals() {
int[][] result = new int[list.size()][]; for (int i = 0; i < list.size(); i++) {
result[i] = list.get(i);
}
return result;
} private int findInsertPosition(int val) {
int left = 0;
int right = list.size(); while (left < right) {
int mid = (left + right) / 2;
if (val >= list.get(mid)[0] && val <= list.get(mid)[1]) return -1; if (val < list.get(mid)[0]) right = mid;
else if (val > list.get(mid)[1]) left = mid + 1;
} return left;
}
} /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* int[][] param_2 = obj.getIntervals();
*/

Java实现 LeetCode 352 将数据流变为多个不相交区间的更多相关文章

  1. LeetCode352 将数据流变为多个不相交区间

    LeetCode352 将数据流变为多个不相交区间 1 题目 给你一个由非负整数 a1, a2, ..., an 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表. 实现 Summa ...

  2. [LeetCode] 352. Data Stream as Disjoint Intervals 分离区间的数据流

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

  3. [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals

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

  4. Java for LeetCode 023 Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. 浅析Spring中AOP的实现原理——动态代理

    一.前言   最近在复习Spring的相关内容,刚刚大致研究了一下Spring中,AOP的实现原理.这篇博客就来简单地聊一聊Spring的AOP是如何实现的,并通过一个简单的测试用例来验证一下.废话不 ...

  2. Git使用教程之新手也能看懂(一)

    首先我写这篇文章的初衷是因为 有一段时间没用Git了,现在突然用起来,很多命令都忘记了,导致去上网查了各种资料和文档(其中廖雪峰老师的文章给我的帮助很大,非常感谢!),花费了大量的时间,等于是又重新学 ...

  3. mysql-kettle-superset电商可视化数据分析

    1.项目概述 需求 对电商业务中的用户.商品.订单的数据进行分析,观察运营的情况 架构 业务数据库:Mysql:存储最原始的数据 ETL:Kettle 数据仓库:Mysql:存储需要进行分析处理的数据 ...

  4. mybatis association的使用

    在上一篇文章中介绍了collection的使用以及java bean,表的结构,今天进行association使用的学习,在多对一的映射关系中,查询到多的一方顺带查询出一的一方是常见的!在此例子中,在 ...

  5. mybatis 自动生成代码工具

    配置官网: http://www.mybatis.org/generator/configreference/xmlconfig.html 源码:https://github.com/mybatis/ ...

  6. 都0202年了,你还不知道javascript有几种继承方式?

    前言     当面试官问你:你了解js哪些继承方式?es6的class继承是如何实现的?你心中有很清晰的答案吗?如果没有的话,可以通过阅读本文,帮助你更深刻地理解js的所有继承方式.       js ...

  7. Asp.net MVC Razor视图模版动态渲染PDF,Razor模版生成静态Html

    Asp.net MVC Razor视图模版动态渲染PDF,Razor模版生成静态Html 1.前言 上一篇文章我开源了轮子,Asp.net Core 3.1 Razor视图模版动态渲染PDF,然后,很 ...

  8. 四使用浮动div布局

    刚开始学习的小白,如有不足之处还请各位补充,感激涕零.在html中有两种方式布局<table>表格和<div>,个人剧的使用表格布局可以避免bug产生,并且表格布局相对来说要容 ...

  9. ubuntu18.04.4安装k8s

    k8s部署 1.集群所有主机关闭swap sudo swapoff -a sudo sed -i 's/.*swap.*/#&/' /etc/fstab 如果重启后swap还是自动挂载执行sy ...

  10. 由一次安全扫描引发的思考:如何保障 API 接口的安全性?

    引言 前段时间,公司对运行的系统进行了一次安全扫描,使用的工具是 IBM 公司提供的 AppScan . 这个正所谓不扫不要紧,一扫吓一跳,结果就扫出来这么个问题. 我们的一个年老失修的内部系统,在登 ...