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种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
随机推荐
- Spring单例 和 Scope注解
关键字 @Scope @Qualifier Singleton 单例 Spring是单例模式.结合Springboot的例子. Controller @Autowired private Tes ...
- APPKIT打造稳定、灵活、高效的运营配置平台
一.背景 美团App.大众点评App都是重运营的应用.对于App里运营资源.基础配置,需要根据城市.版本.平台.渠道等不同的维度进行运营管理.如何在版本快速迭代过程中,保持运营资源能够被高效.稳定和灵 ...
- STP协议树配置
STP协议树作用 为了提高网络可靠性,交换网络中通常会使用冗余链路. 然而,冗余链路会给交换网络带来环路风险 并导致广播风暴以及MAC地址表不稳定等问题进而会影响到用户的通信质量. 生成树协议STP( ...
- @NamedEntityGraphs --JPA按实体类对象参数中的字段排序问题得解决方法
JPA按实体类对象参数中的字段排序问题得解决方法@Entity @Table(name="complaints") @NamedEntityGraphs({ @NamedEntit ...
- Django项目启动之前执行流程剖析
下面,我们只看看主要的步骤: 1.项目启动,遍历settings下面的INSTALLED_APPS,导入默认配置. INSTALLED_APPS = [ 'django.contrib.admin', ...
- Django框架(一)-Django初识
Django初识 一.Web框架本质—自己实现Web框架 1.所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端 import socket sk = sock ...
- BZOJ 2120: 数颜色 带修改的莫队算法 树状数组套主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=2120 标题里是两种不同的解法. 带修改的莫队和普通莫队比多了个修改操作,影响不大,但是注意一下细节 ...
- JBOSS集群和安装
JBOSS集群和安装 http://jijian91.com/blog20071010/jboss-cluster-part5.html http://wing123.iteye.com/blog/3 ...
- scriptlet
<!-- <%! %>:可以修饰全局变量.常量.类.方法 对应java类中的成员变量.常量.内部类.成员方法 --> <%! int num=10;//全局变量 publ ...
- ARM FPGA Extended Memory Interface
Connect a ARM Microcontroller to a FPGA using its Extended Memory Interface (EMI) http://elinux.org/ ...