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. liniux Crontab 的重启和设置

    重启crontab,service cron restart 05 01 * * * /usr/local/php/bin/php FILE 10,30,50 * * * * /usr/local/p ...

  2. bootbox弹出框插件

    具体用法查看官网http://bootboxjs.com/examples.html {% load staticfiles %} <!DOCTYPE html> <html lan ...

  3. windows下nodejs+npm+bower+git+bootstrap组件环境配置

    1.进入nodejs官方网站下载软件(nodejs.org), 2.下载完成后,双击默认安装.安装程序会自动添加环境变量 3.检测nodejs是否安装成功.打开cmd命令行 输入 node - v 显 ...

  4. 模型构建<2>:不平衡样本集的处理

    分类预测建模都有一个基本的假设,即样本集中不同类别的样本个数基本相同,但是在实际任务中,经常会出现各类样本个数差别较大的情况,这样的样本集就是不平衡样本集,它对学习建模的性能会带来很大的影响,因此必须 ...

  5. hdu 2110 基础母函数

    题意:退出本身并不麻烦,麻烦的是,退出的人需要取走相应比例(1/3)金额的资产.假设公司此时一共有n种价值的资产,每种价值的资产数量已知,请帮助心烦意乱的XHD夫妇计算一共有多少种分割资产的方法.   ...

  6. Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈

    C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...

  7. UVALive 5971

    Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it ...

  8. UVALive 6662 TheLastAnt

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  9. NBT(NetBIOS Over TCP)名称解析概述

    在微软IP网络中,客户计算机查找其他计算机并与之进行通信的主要手段是利用域名(DNS).但是,使用先前版本的Windows户机也使用NetBIOS协议,将名称解析为IP地址. 通过三种方法解析NetB ...

  10. ISO7816 (part 1-3) asynchronous smartcard information

    http://java.inf.elte.hu/java-1.3/javacard/iso7816.txt ============================================== ...