数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有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的更多相关文章

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

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

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

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

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

  4. 352. Data Stream as Disjoint Intervals

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

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

  6. 352[LeetCode] Data Stream as Disjoint Intervals

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

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

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

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

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

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

随机推荐

  1. Redis实战

    大约一年多前,公司同事开始使用Redis,不清楚是配置,还是版本的问题,当时的Redis经常在使用一段时间后,连接爆满且不释放.印象中,Redis 2.4.8以下的版本由于设计上的主从库同步问题,就会 ...

  2. Php AES加密、解密与Java互操作的问题

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  3. 3、VS2010+ASP.NET MVC4+EF4+JqueryEasyUI+Oracle项目开发之——用户登录

    近期因为项目赶着上线,一直没时间接着写博客,今天最终空出了时间.声名:我不是专业美工,所以界面问题,希望大家不要拍砖.登录界面例如以下: 在ASP.NET MVC中,要新增一个功能,我们首先要加入一个 ...

  4. Centos 7 yum 安装php

    yum install php php-devel 重启apache使php生效 /etc/init.d/httpd restart 此时可以在目录:/var/www/html/下建立一个PHP文件 ...

  5. QT连接多种数据库f方法及测试

    QT提供了对多种数据库的访问支持,对SQL Server也可以通过ODBC来进行访问.要想顺利访问SQL Server. 首先要保证以下几点:1. QT编译时已经编译了QtSql2. 编译了ODBC插 ...

  6. java_spring_List,Map,Properties,Set注入与遍历

    package com.dao.bean.www; import java.util.List; import java.util.Map; import java.util.Properties; ...

  7. 【大坑】DataGridView多线程更新修改Cell单元格卡死

    最新发现是Column的AutoSizeMode设置为AllCell调整宽度而造成的卡顿,还有就是在现在里面使用Invoke用匿名函数闭包的形式访问For循环的i变量值会不正确导致找不到索引而造成卡顿 ...

  8. java技术栈:一、java编程语言概述

    Java是一种编程语言,起源于20世纪90年代初Sun公司的一个叫Green的项目,该项目主要目的是是开发嵌入家用电器的分布式软件系统,从而使电器更加智能化.因为项目小组成员皆为C++的高手(那个年代 ...

  9. Arcengine10下载地址

    ed2k://|file|%5BArcGIS.Engine.Developer.kit.10.%E8%8B%B1%E6%96%87%E7%89%88%5D.ArcGIS.Engine.Develope ...

  10. 自定义ListView适配器Adapter引用布局文件的情况下实现点击列表项时背景颜色为灰色

    listview控件设置适配器的时候,如果使用自定义的adapter,比如MyArrayAdapter extends ArrayAdapter<String> 如果listitem布局文 ...