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. 从bios启动说起

    如果从bios启动说起的话,BIOS去加载bootloader,bootloader去加载操作系统,那么bootloader是怎么找到操作系统的呢?经过一些流程后,它会去找grub:然后通过grub提 ...

  2. linux下kermit工具的使用

    1.环境: ubuntu16.04 2.背景: 想更换下位机内核 3.使用kermit进行串口传输 举例:传输文件到下位机 2.1首先进入下位机的uboot 2.2 使用uboot自带的命令从串口接收 ...

  3. 垒骰子|2015年蓝桥杯B组题解析第九题-fishers

    垒骰子 赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体. 经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥! 我们先来规范一下骰子:1 的 ...

  4. [SpringBoot] - 一份笔记

    一. Spring Boot 入门 1. Spring Boot 简介 简化Spring应用开发的一个框架; 整个Spring技术栈的一个大整合; J2EE开发的一站式解决方案; 2. 微服务 201 ...

  5. Linux——shell简单学习(二)

    流控制语句: for…done语句 格式:for  变量   in   名字表 do  命令列表 done 例子: #!/bin/sh for DAY in Sunday Monday Tuesday ...

  6. MySQL数据库调优技巧

    原文链接:https://m.aliyun.com/bbs/read/300762.html MySQL是一个功能强大的开源数据库.随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它 ...

  7. html 居中的内容显示框

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Could not find a package configuration file provided by 'ecl_threads' ,.................couldn't find required component 'ecl_threads'

    sudo apt-get install ros-kinetic-ecl-threads

  9. MAC OS 英语朗读功能

    哈哈哈,太神奇了 在命令行中敲say + word ,系统能够自己讲word读出来. 如果是敲的是 say +中文, 就不知道再读什么啦 哈哈哈哈---- 此外,在对应网站选中内容后还可以右击,用sp ...

  10. springboot入门(一)--快速搭建一个springboot框架

    原文出处 前言在开始之前先简单介绍一下springboot,springboot作为一个微框架,它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架 ...