data-stream-as-disjoint-intervals
https://leetcode.com/problems/data-stream-as-disjoint-intervals/
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class SummaryRanges {
vector<Interval> vec;
int find(int val) {
int vlen = vec.size();
if (vlen == ) {
return -;
}
int begin = ;
int end = vlen - ;
int mid;
while (begin <= end) {
mid = begin + (end - begin) / ;
// Here <= vs. >
if (vec[mid].start > val) {
end = mid - ;
}
else {
begin = mid + ;
}
}
// Here it's important to return end
return end;
} public:
/** Initialize your data structure here. */
SummaryRanges() { } void addNum(int val) {
int idx = find(val);
if (idx < ) {
if (vec.size() > && vec[].start == val+) {
vec[].start = val;
}
else {
Interval inter(val, val);
vec.insert(vec.begin(), inter);
}
}
else {
if (vec[idx].end == val-) {
vec[idx].end = val;
}
else if (vec[idx].end < val-) {
Interval inter(val, val);
vec.insert(vec.begin()+idx+, inter);
idx++;
}
// check latter
if (vec.size() > idx+ && vec[idx+].start == vec[idx].end + ) {
vec[idx].end = vec[idx+].end;
vec.erase(vec.begin()+idx+);
}
}
} vector<Interval> getIntervals() {
return vec;
}
}; /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* vector<Interval> param_2 = obj.getIntervals();
*/
data-stream-as-disjoint-intervals的更多相关文章
- [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 ...
- 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 ...
- [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- 352[LeetCode] Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- 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 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [leetcode]352. Data Stream as Disjoint Intervals
数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
随机推荐
- thinkphp每次跳转时都会显示笑脸的修改
Success和error方法都有对应的模板,并且是可以设置的,默认的设置是两个方法对应的模板都是://默认错误跳转对应的模板文件'TMPL_ACTION_ERROR' => THINK_PAT ...
- redis集群错误解决:/usr/lib/ruby/gems/1.8/gems/redis-3.0.0/lib/redis/client.rb:79:in `call': ERR Slot 15495 is already busy (Redis::CommandError)
错误信息: /usr/lib/ruby/gems/1.8/gems/redis-3.0.0/lib/redis/client.rb:79:in `call': ERR Slot 15495 is al ...
- 使用命令行管理virtualBox
最近在鼓捣hadoop,装了几台虚拟机,,总感觉gui启动很别扭,后来发现virtualBox有个headless模式,只想说舒服! 常用命令 VBoxManage startvm name|id - ...
- sqlldr errors
sqlldr myUser/myPWD@myCONN control='d:/sqlload/new/test/loader1.ctl' errors=1000000
- spring 事务配置
事务配置文档xml <!-- from the file 'context.xml' --> <?xml version="1.0" encoding=" ...
- Mac 上自带TFTP Server 软件的使用
搬瓦工搭建SS教程 1.TFTP协议 简单文件传输协议Trivial File Transfer Protocol (TFTP)是一个基于UDP协议的简单的.低开销的文件传输协议,允许客户端get或者 ...
- kotlin 视频
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha
- bzoj 1336 最小圆覆盖
最小圆覆盖 问题:给定平面上的一个点集,求半径最小的一个圆,使得点集中的点都在其内部或上面. 随机增量算法: 定义:点集A的最小圆覆盖是Circle(A) 定理:如果Circle(A)=C1,且a不被 ...
- java值和地址值传递、字符串常量池的理解
#java值和地址值传递的理解: - 基本数据类型和基本数据类型的封装类都是:值传递 * 形式参数的改变不会影响实际参数的改变(相当于将值复制一份传递给形参,自身没做任何改变) - 引用数据 ...
- trigger、procedure和event如何同步
最近遇到一个需求涉及存储过程,被突然问题到如何同步问题问到了,赶紧补课学习一下. 首先,先看一下trigger.procedure和event的定义都是什么? trigger: 触发器是一个被指定关联 ...