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. linux性能分析工具之火焰图

    一.环境 1.1 jello@jello:~$ uname -a Linux jello 4.4.0-98-generic #121-Ubuntu SMP Tue Oct 10 14:24:03 UT ...

  2. SxsTrace

    https://troubleshooter.xyz/wiki/fix-the-application-has-failed-to-start-because-the-side-by-side-con ...

  3. 【第三十六章】 metrics(4)- metrics-graphite

    将metrics report给graphite(carbon-relay) 一.代码 1.pom.xml <!-- metrics-graphite --> <dependency ...

  4. .NET MVC请求流程

    ASP.NET MVC 请求流程:Controller MvcHandler Action Action参数赋值 .NET MVC权限设计思考之切入点

  5. hdoj-2086-A1=?

    题目:A1=? 代码(作者:Fistice): #include<cstdio> #include<cmath> #include<cstring> #includ ...

  6. UVa 1451 平均值

    https://vjudge.net/problem/UVA-1451 题意:给定长度为n的01串,选一个长度至少为L的连续子串,使得子串中数字的平均值最大. 思路:这题需要数形结合,真的是很灵活. ...

  7. codeforces 766E Mahmoud and a xor trip

    题目链接:http://codeforces.com/problemset/problem/766/E 大意,给出一个$n$个点的树,每个点都有一个权值,令$Disx$为$u$到$v$路径上的异或和求 ...

  8. c++ 匹配A容器中最先出现的b容器中的元素,返回iterator,(find_first_of)

    #include <iostream> // std::cout #include <algorithm> // std::find_first_of #include < ...

  9. [ios]object-c math.h里的数学计算公式介绍

    参考:http://blog.csdn.net/yuhuangc/article/details/7639117 头文件:<math.h> 1. 三角函数  double sin (dou ...

  10. ESXi6.7安装流程和bug处理

    ·前言 ·准备工作 ·安装 ·Initializing IOV卡住 ·缺少网卡驱动 ·安装ESXi6.7 ·Multiboot could not setup the video subsystem ...