Insert Interval

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

 
 
 /**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) { int n=intervals.size();
vector<Interval> result; if(n==)
{
result.push_back(newInterval);
return result;
} int start=newInterval.start;
int end=newInterval.end; bool isfindStart=false;
bool isfindEnd=false;
bool addCur=true; for(int i=;i<n;i++)
{
addCur=true; if(!isfindStart)
{
//和当前区间没有交叉,且在start的前面
if(start>intervals[i].end)
{
//需要添加当前元素
addCur=true;
//当前元素为最后一个元素时,则认为start找到了
if(i==n-) isfindStart=true;
}
else
{
//和当前区间有交叉 或者start小于当前区间 start=min(start,intervals[i].start);
//后续就不用再找start了
isfindStart=true; //如果start和end没有和其他Interval有交叉
if(end<intervals[i].start) addCur=true;
else addCur=false;
}
} if(!isfindEnd)
{
//需要继续找end
if(end>intervals[i].end)
{ if(start<=intervals[i].end) addCur=false; if(i==n-)
{
isfindEnd=true; if(addCur) result.push_back(intervals[i]);
result.push_back(Interval(start,end)); continue;
}
}
else
{
//end小于当前区间,此时找到了end
end=end>=intervals[i].start?intervals[i].end:end; //找到了end
isfindEnd=true;
if(end>=intervals[i].start) addCur=false; result.push_back(Interval(start,end));
if(addCur) result.push_back(intervals[i]); continue;
}
} if(addCur) result.push_back(intervals[i]);
}
return result;
}
};

【leetcode】Insert Interval的更多相关文章

  1. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

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

  2. 【leetcode】Insert Interval(hard)★

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

  3. 【Leetcode】【Hard】Insert Interval

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

  4. 【LeetCode】986. Interval List Intersections 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetco ...

  5. 【leetcode】986. Interval List Intersections

    题目如下: Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted ...

  6. 【leetcode】986. Interval List Intersections (双指针)

    You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, ...

  7. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

  8. 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)

    [LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  9. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

随机推荐

  1. C-全局变量与局部变量

  2. SpringMVC框架的工作原理

    学习SpringMVC的工作原理,首先有三个要解决的问题: (1)DispathcherServlet框架如何截获特定的HTTP请求,交由SpringMVC处理? (2)位于Web层的Spring容器 ...

  3. Java Programming Test Question 2

    public class JPTQuestion2 { public static void main(String[] args) { String s3 = "JournalDev&qu ...

  4. zend stuido 12.5的插件安装和xdebug调试器的配置和和配置注意

    参考: zend stuido 12.5的插件安装 zend 12.5 安装插件是按类别进行分类了的, 而且是在欢迎 界面就可以直接安装, 安装后,要重启zend才能生效 版式设计的一个基本点就是: ...

  5. 【C语言入门教程】4.4 指针 与 指针变量

    在程序中声明变量后,编译器就会为该变量分配相应的内存单元.也就是说,每个变量在内存会有固定的位置,有具体的地址.由于变量的数据类型不同,它所占的内存单元数也不相同.如下列声明了一些变量和数组. int ...

  6. WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据

    WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据 WebForm1.aspx 页面 (原生AJAX请求,写法一) <%@ Page Langu ...

  7. 批处理快速更改ip地址

    在各种网络中切换,windows更换ip地址步骤:  进入控制面板--网络和internet--网络和共享中心--理性适配器设置--然后找到网卡--进入属性--然后internet 协议--更改ip信 ...

  8. uMlet建模工具

    下载:http://www.umlet.com/ 无意中发现的一款建模工具,能快速搭建数据库模型,前置安装条件是java环境. 这是我建的user模型表,2个字段name和age,2个方法getAge ...

  9. 正则表达式30分钟入门:http://deerchao.net/tutorials/regex/regex.htm#mission

    http://deerchao.net/tutorials/regex/regex.htm#mission

  10. Markdown入门教程

    Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdown 的语法十分简单.常用的标记符号也不 ...