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 ...
随机推荐
- 用Stream流轻易的收集数据
前言 在日常使用集合时,我们通常使用迭代器来处理集合中的数据,假如有一个用户列表 List,我们想要将用户按照性别分组生成 Map<String, List>.需要遍历 List,然后判断 ...
- [hdu5204]水题
思路:插入的数按指数级增长,所以范围内最多存在logR个数.并且最近i次插入的数,首位置为2^(i-1),且每隔2^i出现一次,于是暴力之..可以用插入排序维护,也可查询时再排下序. 一: #prag ...
- SpringBoot基础实战系列(一)整合视图
SpringBoot整合freemarker 1.添加依赖:springboot基本上是无缝衔接,基本上只需要添加对应的依赖,不需要或者做很少量的配置即可 注:对于springboot项目的创建此处不 ...
- HDU 2017 (水)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2017 题目大意:给你段字符串,求出字符串中含有数字字符的个数 解题思路: 字符串输入输出的基本应用:h ...
- android progressbar 自定义图片匀速旋转
项目中需要使用圆形进度条进行数据加载的显示,所以需要两个步骤 1:自定义progressbar滚动图片 2:匀速旋转图片 步骤一:自定义progressbar图片 <ProgressBar an ...
- js使用经验--if...else简化
目的 在项目中,if else语句如果用得很多,特别是嵌套,代码不美观,阅读性不好.所以的话,用其他的方式简化替换if...else...就很有必要. 简化的作用就是赠人玫瑰,手留余香.对自己对项目对 ...
- My sql的知识点 不足点请指点谢谢
My SQL:(关系数据库) 数据库能够能够干吗? 1. :存储大量的信息,方便检索和访问 2. :保持数据信息的一致,完整 3. : 共享和安全 4. : 通过组合分析,产生新的有用 ...
- IOS真机测试(已拥有个人开发者证书)
创建真机调试证书并进行真机测试 步骤1 在启动台中点击其他,找到钥匙串访问. 步骤2 在打开的界面中点击右边的系统根证书,然后点击左上角的钥匙串访问,然后是证书助理,最后点击从证书颁发机构申请证书. ...
- 【雕爷学编程】MicroPython动手做(07)——零基础学MaixPy之机器视觉
机器视觉 machine vision机器视觉是人工智能正在快速发展的一个分支.机器视觉作为生产过程中关键技术之一,在机器或者生产线上,机器视觉可以检测产品质量以便将不合格的产品剔除,或者指导机器人完 ...
- POJ1948 Triangular Pastures
POJ1948 Triangular Pastures #include <iostream> #include <cmath> using namespace std; ; ...