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. JAVA 注解的几大作用及使用方法详解

    JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...

  2. python 多线程就这么简单

    原文地址:http://www.cnblogs.com/fnng/p/3670789.html

  3. 使用VMware 安装Linux CentOS7

    访问百度经验 http://jingyan.baidu.com/article/eae0782787b4c01fec548535.html 安装无忧..

  4. 配置文件操作模块,configparser

    configparser configparser用于处理特定格式的文件,其本质上是利用open来操作文件. # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 ...

  5. 多线程 GET

    iOS中多线程的实现 方案 简介 语言 线程生命周期 使用频率 pthread 一套通用的多线程API 适用于 Unix / Linux / Windows 等系统 跨平台\可移植 使用难度大 C 程 ...

  6. URAL1826. Minefield 题解

    原题: http://acm.timus.ru/problem.aspx?space=1&num=1826 1826. MinefieldTime limit: 0.5 secondMemor ...

  7. hdu4751 Divide Groups

    This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is goi ...

  8. elf文件中的.plt .rel.dyn .rel.plt .got .got.plt的关系

    .plt的作用是一个跳板,保存了某个符号在重定位表中的偏移量(用来第一次查找某个符号)和对应的.got.plt的对应的地址 .rel.dyn重定向表,在程序启动时就需要重定位完成. .rel.plt保 ...

  9. nyoj 171 聪明的kk

    聪明的kk 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 聪明的“KK”非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘,体现出本国不断变换和绚丽多彩的 ...

  10. EF接触02

    Ado.net Entity Framework早期称为ObjectSpace.基于Ado.net操作数据库的一组类库. 什么是ADO.NET? 基础.net平台下的操作数据库的一组Api或组建.五大 ...