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的更多相关文章

  1. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  2. 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 ...

  3. 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 ...

  4. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  5. 352. Data Stream as Disjoint Intervals

    Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...

  6. [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  7. 352[LeetCode] Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  8. 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 ...

  9. [LeetCode] 352. Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  10. [leetcode]352. Data Stream as Disjoint Intervals

    数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...

随机推荐

  1. MVC、MVP和MVVM的异同

    No1: Model一般用来保持程序的数据状态,比如数据存储.网络请求等 No2: Android开发中应用到MVC的地方:比如ListView与Adapter,如果把ListView看作View层, ...

  2. poj-2528线段树练习

    title: poj-2528线段树练习 date: 2018-10-13 13:45:09 tags: acm 刷题 categories: ACM-线段树 概述 这道题坑了我好久啊啊啊啊,,,, ...

  3. Ninject

    一.为什么要使用依赖注入框架 依赖注入框架也叫IoC容器.它的作用使类与类之间解耦 我们看看为什么要用依赖注入框架,举个几个梨子: 1,高度耦合的类 有一个Order类,Order类是用于订单操作的, ...

  4. 1008 Elevator (20)(20 point(s))

    problem The highest building in our city has only one elevator. A request list is made up with N pos ...

  5. OpenContrail 体系

    OpenContrail 体系架构文档 1  概述1.1  使用案例1.2  OpenContrail控制器和vRouter1.3  虚拟网络1.4     Overlay Networking1.5 ...

  6. thinkphp的_STORAGE_WRITE_ERROR_问题

    今天服务器突然报这个问题(上图所示),在thinkphp的官网上也发现有朋友碰到这个问题,定位到应该是Runtime目录没有写权限,然后试着给Application下的Runtime目录 chmod ...

  7. sql分组排序取top

    写法1: use anypay; select tr.* from (select task_code, max(created_at) as cal from task_log group by t ...

  8. 【线段树】【扫描线】Petrozavodsk Winter Training Camp 2018 Day 5: Grand Prix of Korea, Sunday, February 4, 2018 Problem A. Donut

    题意:平面上n个点,每个点带有一个或正或负的权值,让你在平面上放一个内边长为2l,外边长为2r的正方形框,问你最大能圈出来的权值和是多少? 容易推出,能框到每个点的 框中心 的范围也是一个以该点为中心 ...

  9. hihocoder #1299 : 打折机票 线段树

    #1299 : 打折机票 题目连接: http://hihocoder.com/problemset/problem/1299 Description 因为思念新宿的"小姐姐"们, ...

  10. Pycharm报错解决:error:please select a valid Python interpreter

    问题描述: 之前PC上安装的是Python2,后来工作需要转成Python3了.然后在用pycharm运行Python2的程序时发现源程序运行报错(出去语法错误) error:please selec ...