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. MySQL命令行导出、导入数据库,备份数据库表

    MySQL导出数据库/数据表 1.首先,将你MySQL安装目录,例如C:\Program Files\MySQL\MySQL Server 5.7\bin添加到你的系统环境变量PATH中: 2.导出数 ...

  2. 打开vi后提示The ycmd server SHUT DOWN (restart with :YcmRestartServer)该如何处理

    答:进入YouCompleteMe的安装目录安装一些必要的依赖 比如:笔者将YouCompleteMe安装到了~/.vim/bundle目录下,那么执行以下操作: cd ~/.vim/bundle/Y ...

  3. openwrt如何单独编译uboot

    答:make package/boot/uboot-<chip series>/compile

  4. sql 之优化小技巧

    SET NOCOUNT ON:不返回计数,如果存储过程中包含一些并不返回实际数据的语句,网络通信流量便会大量减少,可以显著提高应用程序性能:

  5. java中子类实例化过程中的内存分配

    知识点: 子类继承父类之后,实例化子类时,内存中子类是如何分配内存的呢? 下面,自己会结合一个例子,解释一下,一个子类实例化过程中,内存是如何分配的 参考博客:http://www.cnblogs.c ...

  6. StringUtils类常用的方法讲解

    StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 ...

  7. pycaffe编译

    环境:ubuntu14.04  python2.7  caffe已经成功编译 1,首先确保pip已经安装 sudo apt-get install python-pip 2,在caffe-master ...

  8. UVa 225 黄金图形(回溯+剪枝)

    https://vjudge.net/problem/UVA-225 题意:平面上有k个障碍点,从(0,0)出发,第一次走1个单位,第二次走2个单位,...第n次走n个单位,最后恰好回到(n,n).每 ...

  9. C# asp.net 比较两个时间的差求天数

    string str1 = "2017-2-13 23:59:59"; string str2 = "2017-2-14 0:00:01"; DateTime ...

  10. poj 2385 Apple Catching 基础dp

    Apple Catching   Description It is a little known fact that cows love apples. Farmer John has two ap ...