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. [hdu5387 Clock]时钟夹角问题

    题意:给一个时刻,求时针.分钟.秒针三者之间的夹角 思路:确定参照点,求出三者的绝对夹角,然后用差来得到它们之间的夹角,钝角情况用360.减去就行了. #include <map> #in ...

  2. vscode+eslint自动格式化vue代码的方法

    前言 使用vscode开发vue项目的时候,为了编码格式的统一化,使用eslint规范进行格式化.此时通过eslint插件可以实现对vue代码的自动格式化. 使用方式 在vscode的插件模块处,搜索 ...

  3. C:复试

    C语言程序设计基础知识 C语言特点 1.是一种兼有高级语言和汇编语言优点的语言 2.是一种结构化程序设计语言 3.数据类型丰富 4.具有丰富的运算符 5.具有预处理功能 合理算法的特点 1.有输入 2 ...

  4. windows电脑关闭自动更新的方法

    第一步.打开我的电脑,点击此电脑,然后点击管理 第二步.在计算机管理(本地)里面找到服务和应用程序,点击进入 第三步.进去,点击服务 第四步.往下滑,找到windows update,点击进入 第五步 ...

  5. 我的linux学习日记day5

    一.vim 编辑器 有三种模式,命令模式,输入模式,末行模式 1.下面是命令模式常用的命令 2.末行模式常用命令 :w 保存 :q 退出 :q! 强制退出 :wq! 强制保存退出 :set nu 显示 ...

  6. zsy后台管理系统-界面

    自研平台:(java+vue+mysql+docker) 欢迎大家预览,指导! http://www.zsyai.top/dist 流程引擎 自定义定时任务: 一键生成前后端代码:

  7. Spring Boot集成H2数据库

    需求 平时学习的时候,涉及到一些连接数据库相关的操作,经常需要初始化本地数据库,比如装个MySQL,初始化一些脚本,比较麻烦,H2是内存数据库,Spring Boot可以在应用启动的时候对H2数据库初 ...

  8. Python--WebDriverWait+expected_conditions的一个应用

    from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.s ...

  9. DRF版本组件

    1.DRF版本组件配置类局部使用 局部配置: 方式一:直接配置versioning_class=QueryParameterVersioning 方式二:直接配置versioning_class=UR ...

  10. Netty 中的消息解析和编解码器

    本篇内容主要梳理一下 Netty 中编解码器的逻辑和编解码器在 Netty 整个链路中的位置. 前面我们在分析 ChannelPipeline 的时候说到入站和出站事件的处理都在 pipeline 中 ...