Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

  类似以前的merge Intervals,只不过这里实际上是要将一个Interval插入到内部之后,然后再merge一下
而且这里的intervals在这里首先是已经排好序了的:

首先是一个带二分搜索的C++的方法:

 class Solution {
public:
static bool comp(Interval a,Interval b){
return a.start<b.start;
}
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
if(intervals.size()==){
intervals.push_back(newInterval);
return intervals;
}
// sort(intervals.begin(),intervals.end(),comp);
if(intervals[].start>newInterval.end){
intervals.insert(intervals.begin(),newInterval);
return intervals;
}
else if(intervals[intervals.size()-].end<newInterval.start){
intervals.push_back(newInterval);
return intervals;
}
int left = binaryS(newInterval.start,intervals,,intervals.size()-, true);
int right = binaryS(newInterval.end, intervals,left, intervals.size()-,false);
int delLeft,delRight;
cout<<left<<right;
if(left == && intervals[].start>newInterval.start){
delLeft = left;
}
else if(intervals[left].end>= newInterval.start){
newInterval.start = intervals[left].start;
delLeft = left;
}
else{
delLeft = left+;
} if(right == intervals.size()- && intervals[right].end<newInterval.end){
delRight = right;
}
else if(intervals[right].start<= newInterval.end){
newInterval.end = intervals[right].end;
delRight = right;
}
else{
delRight = right-;
}
cout<<delLeft<<" "<<delRight<<endl;
vector<Interval> result; for(int i=;i<delLeft;i++){
result.push_back(intervals[i]);
}
result.push_back(newInterval);
for(int i=delRight+;i<intervals.size();i++){
result.push_back(intervals[i]);
}
return result;
// for(int i=delLeft;i<=delRight;i++){
// intervals.erase(intervals.begin()+delLeft);
// }
// intervals.push_back(newInterval);
// return intervals;
}
int binaryS(int x, vector<Interval> & intervals, int low, int high, bool isStart){
if(isStart){ while(low < high){
int mid = (low + high + ) /;
cout<<low<<high;
if(intervals[mid].start <= x){
low = mid;
}
else{
high = mid-;
}
}
}
else{
while(low < high){
int mid = (low + high) /;
// cout<<low<<high;
if(intervals[mid].end < x){
low = mid+;
}
else{
high = mid;
}
}
}
return low;
}
};

java版本的代码如下所示:

 /**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> ret = new ArrayList<Interval>();
int sz = intervals.size();
if(sz == 0){
ret.add(newInterval);
return ret;
}
int prev = 0, next = 1;
while(next < sz){
if(newInterval != null && intervals.get(prev).end >= newInterval.start){
intervals.get(prev).end = Math.max(intervals.get(prev).end, newInterval.end);
newInterval = null;
}else if(newInterval == null){
if(intervals.get(prev).end >= intervals.get(next).start){
intervals.get(prev).end = Math.max(intervals.get(prev).end, intervals.get(next).end);
next++;
}else{
ret.add(intervals.get(prev));
prev = next;
next++;
}
}else{
ret.add(intervals.get(prev));
prev++;
next++;
}
}
intervals.get(prev).end = Math.max(intervals.get(prev).end, intervals.get(sz-1).end);
ret.add(intervals.get(prev));
return ret;
}
}

LeetCode OJ:Insert Interval的更多相关文章

  1. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  2. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  3. 【LeetCode】57. Insert Interval [Interval 系列]

    LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间;  3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...

  4. 【LeetCode】57. Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  5. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. LeetCode OJ:Search Insert Position(查找插入位置)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  7. 【一天一道LeetCode】#57. Insert Interval

    一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...

  8. [leetcode sort]57. Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  9. LeetCode OJ:Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

随机推荐

  1. shell脚本中如何使scp不输入密码即可传输文件

    答:使用ssh密钥对 示例如下: 如果A机想要获取B机上的文件,那么需要将在A机上生成的公钥放置到B机上的指定位置~/.ssh/authorized_keys 问题一: 如何在A机上生成ssh密钥对? ...

  2. linux下查找指定后缀的文件

    1.linux下查找指定后缀的文件 例如查找当前目录下的所有后缀名时.c或.h的文件 find  .  -type f -regex  ".*\.\(c\|h\)"

  3. C Looooops(扩展欧几里得)题解

    A Compiler Mystery: We are given a C-language style for loop of type  for (variable = A; variable != ...

  4. python 将16进制转化为2进制

    >>> x='123abc' >>> b=bin())[:] >>> print(b)

  5. python tcp

    server import socket host="localhost" port= s=socket.socket(socket.AF_INET,socket.SOCK_STR ...

  6. Jmeter 分布式压力测试

      JMeter中进行分布式测试 作为一个纯 JAVA 的GUI应用,JMeter对于CPU和内存的消耗还是很惊人的,所以当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心, ...

  7. EsayUI + MVC + ADO.NET(仓储基类)

      该篇主要讲解工作单元的运用 和DbHlper抽象封装 工作单元仓储基类:BaseRepository (DbHlper抽象封装)  仓储接口(CRUD):IRepository (CRUD接口) ...

  8. tomcat日志切割和定期删除

    tomcat日志切割和定期删除 在tomcat的软件环境中,如果我们任由日志文件无限增长,总有一天会将磁盘占满的(废话).特别是在日志文件增长速度很快的一些情况下,按日志切割日志文件并删除,就是一件很 ...

  9. B2B、B2C、C2C、O2O 和 P2P 的含义

    B2C(Business-to-Customer)商家对客户 我开一家公司卖东西,你来买,即B2C.生活中常用的比如我们经常在天猫旗舰店上面购物,天猫入驻的都是商家,而我们买东西的就是客户,这就是B2 ...

  10. 2017-03-03 Oracle在.Net中出现未在本地计算机上注册“OraOLEDB.Oracle”提供程序的错误

    在前面的Oracle配置完成后,打开项目运行出错,出现未在本地计算机上注册“OraOLEDB.Oracle”提供程序的错误,看到“注册”两个字,首先想到,难道还要用命令行注册一下?果不其然,需要手动注 ...