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种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
随机推荐
- (转)看懂UML类图
转自:http://design-patterns.readthedocs.io/zh_CN/latest/read_uml.html 这里不会将UML的各种元素都提到,我只想讲讲类图中各个类之间的关 ...
- Java使用AES加密解密
AES加密机制: 密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准. 这个标准用来替代原先的 ...
- Python面向对象中super用法与MRO机制
1. 引言 最近在研究django rest_framework的源码,老是遇到super,搞得一团蒙,多番查看各路大神博客,总算明白了一点,今天做一点总结. 2. 为什么要用super 1)让代码维 ...
- [Python网络编程]一个简单的TCP时间服务器
服务器端: 1.创建一个面向网络的TCP套接字对象socket, 2.绑定地址和端口 3.监听 4.当有客户端连接时候,接受连接并给此连接分配一个新的套接字 5.当客户端发送空信息时候,关闭新分配的套 ...
- Xamarin 2017.10.9更新
Xamarin 2017.10.9更新 本次更新主要解决了一些bug.Visual Studio 2017升级到15.4获得新功能.Visual Studio 2015需要工具-选项-Xamarin ...
- Jquery的方法(一)
一.文档操作1.内部插入:append(),appendTo(),prepend():2.外部插入:after(),before():3.删除操作:remove(),empty():4.克隆操作:cl ...
- 同步VDP时间
使用yast 进入蓝屏界面,修改system—date and time,取消hardware clock set to utc,时区设置为上海或者北京,然后sntp -r 时间服务器地址 敲击syn ...
- 2 Spring4 之Bean的配置
Spring4 之Bean的配置 1 IOC & DI 概述 IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源 ...
- Codeforces Round #298 (Div. 2) E. Berland Local Positioning System 构造
E. Berland Local Positioning System Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- Python— isinstance用法说明
在学习自动化测试的脚本中发现了这个函数,所以在网上查了一下资料进行如下整理: 通过帮助查看如下: 作用:来判断一个对象是否是一个已知的类型: 其第一个参数(object)为对象,第二个参数为类型名(i ...