[leetcode]352. Data Stream as Disjoint Intervals
数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样提交了,发现错了,想到之前考虑要不要判重,我感觉是这个问题,然后就是在二分查找的时候,判断一下左右区间是否包含当前的值,包含就直接返回。
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool cmp(Interval a, Interval b) {
if(a.start == b.start) return a.end < b.end;
return a.start < b.start;
}
class SummaryRanges {
public:
/** Initialize your data structure here. */ vector<Interval> v;
SummaryRanges() {
v.clear();
} void addNum(int val) {
//cout << val << " " << v.size() << endl;
Interval d(val, val);
if(v.size() == ) v.push_back(d);
else {
int t = lower_bound(v.begin(), v.end(), d, cmp) - v.begin();
//cout << val << " " << t << endl;
if(t == ) {
if(val == v[].start) return;
if(v[].start - == val) {
v[].start = val;
} else {
v.insert(v.begin() + t, d);
}
} else if(t == v.size()) {
if(v[t - ].end >= val) return;
if(v[t - ].end + == val) {
v[t - ].end = val;
} else {
v.push_back(d);
}
} else {
if(v[t - ].start == val || v[t - ].end >= val || v[t].start == val) return;
if(v[t - ].end + == v[t].start) {
v[t - ].end = v[t].end;
v.erase(v.begin() + t);
} else if(v[t - ].end + == val) {
v[t - ].end = val;
} else if(v[t].start - == val) {
v[t].start = val;
} else {
v.insert(v.begin() + t, d);
}
} }
} vector<Interval> getIntervals() {
return v;
}
}; /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* vector<Interval> param_2 = obj.getIntervals();
*/
[leetcode]352. Data Stream as Disjoint Intervals的更多相关文章
- [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 (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 ...
- 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 ...
- 352[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 分离区间的数据流
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 ...
- [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
随机推荐
- 用 C# 做人脸检测(EmguCV)
用 C# 做人脸检测(EmguCV) 原发:bbs.csdn.net 作者:野比 (conmajia@gmail.com) 时间:May 2012 下载源码 准备工作 下载 EmguCV 傻瓜安装 ...
- 从经典问题来看 Copy 方法(转)
来自:Gua | 瓜地 链接:https://desgard.com/copy/ 在初学 iOS 的时候,可能会被灌输这么一个常识,切记 NSString 的 property 的修饰变量要写作 c ...
- linux系统基础(二)
磁盘管理(一) Linux设备认识 /dev/cdrom /dev/sr0 /dev/mouse /dev/sda /dev/hda IDE硬盘(支持4块):hd(a-d) [非IDE硬盘]SCSI硬 ...
- Struts2 Action的访问路径
1. Action的访问路径 扩展名 缺省以.action结尾,请参考:default.properties文件,可以通过配置改变这一点: <constant name="st ...
- 笨办法学C 练习
http://c.learncodethehardway.org/book/index.html
- CCCatmullRomTo&CCCatmullRomBy
注: 云形线(Catmull-Rom curve曲线) 云线(Spline或B-spline)在数学上有很多种类,常用的三阶云线有Hermite, Bezier, Uniform B-spline, ...
- java RSA加密解密--转载
原文地址:http://www.blogjava.net/icewee/archive/2012/05/19/378570.html 该工具类中用到了BASE64,需要借助第三方类库:javabase ...
- XMemcached使用示例--转
Memcached 是一个高性能的分布式内存对象的key-value缓存系统,用于动态Web应用以减轻数据库负载,现在也有很多人将它作为内存式数据库在使用,memcached通过它的自定义协议与客户端 ...
- Java基础知识强化之多线程笔记05:Java程序运行原理 和 JVM的启动是多线程的吗
1. Java程序运行原理: Java 命令会启动Java 虚拟机,启动 JVM,等于启动了一个应用程序,也就是启动了一个进程.该进程会自动启动一个 “主线程” ,然后主线程去调用某个类的 m ...
- MySql添加用户,新建数据库,用户授权,删除用户,修改密码
转自:http://www.cnblogs.com/fly1988happy/archive/2011/12/15/2288554.html MySql中添加用户,新建数据库,用户授权,删除用户,修改 ...