Java实现 LeetCode 352 将数据流变为多个不相交区间
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 将数据流变为多个不相交区间的更多相关文章
- LeetCode352 将数据流变为多个不相交区间
LeetCode352 将数据流变为多个不相交区间 1 题目 给你一个由非负整数 a1, a2, ..., an 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表. 实现 Summa ...
- [LeetCode] 352. Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- 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. 解 ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【基础】excel如何根据数据内容显示不同颜色。
需求: 店柜完成率排名相比上阶段升降,升显示绿色“↑“,降显示红色“↓”,持平显示黑色“-”. 步骤: 第一步 先计算两次排名的差值(本次排名-上次排名). 第二步 对差值列设置单元格格式,设置格式如 ...
- failed parsing overlays.
clearn + rebuild + 重新运行: 删掉模拟器进程 + 重新运行:
- 宝塔webHook自动同步代码的使用
#!/bin/bashecho ""#输出当前时间date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"echo " ...
- android Viewpager禁用/开启滑动切换功能
要实现viewpager的滑动以及禁止滑动切换功能只需要继承viewpager,在onTouchEvent进行逻辑判断即可(网上搜到的,确实可行,原创地址不明),下面自己实现一个 import and ...
- Java 集合排序策略接口 Comparator
1. 前言 最近用到了集合排序(基于 Java 8).现在我能用 Stream 的就用 Stream ,真香!排序可以这么写: List<People> peoples = new Arr ...
- git branch分支
推荐看这个教程: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013758 ...
- 「雕爷学编程」Arduino动手做(30)——光敏二极管模块
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(动手做)的理念,以学习和交流为目的,这里准备逐 ...
- day09作业01用户登录与验证
import timeLoginTime = time.asctime( time.localtime(time.time()) )print ("time %s" % Login ...
- linux ,mac连接, git pull error, chmod修改文件的权限/chown修改文件和目录的所有者
去项目目录下 启动服务 setsid npm start & Mac下如何用SSH连接远程Linux服务器 https://www.cnblogs.com/littleBit/p/536280 ...
- 代码行数统计的Java和Python实现
通过编写程序来统计文件的行数,可以在巩固文件IO知识的同时计算出自己的代码量,以下分别提供Java和Python实现的版本. 解决思路 两种版本的思路几乎相同,每一个文件夹(目录)内的行数都是其所有子 ...