原题地址

遍历每个区间intervals[i]:

如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result

如果intervals[i]在newInterval的右边,且没有交集,如果newInterval还没插入,则将newInterval插入result,之后再插入intervals[i]

如果intervals[i]和newInterval有交集,则更新newInterval的start、end

代码:

 vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> result;
bool inserted = false; for (auto itv : intervals) {
if (itv.end < newInterval.start)
result.push_back(itv);
else if (itv.start > newInterval.end) {
if (!inserted) {
result.push_back(newInterval);
inserted = true;
}
result.push_back(itv);
}
else {
newInterval.start = min(itv.start, newInterval.start);
newInterval.end = max(itv.end, newInterval.end);
}
}
if (!inserted)
result.push_back(newInterval); return result;
}

Leetcode#57 Insert Interval的更多相关文章

  1. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  2. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

  3. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  4. [LeetCode] 57. Insert Interval 插入区间

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

  5. LeetCode: 57. Insert Interval(Hard)

    1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...

  6. LeetCode 57. Insert Interval 插入区间 (C++/Java)

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  7. [LeetCode] 57. Insert Interval 解决思路

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

  8. [leetcode]57. Insert Interval插入区间

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

  9. Leetcode 57: Insert Interval 让代码更好读, 更容易测试.

    阅读了几个博客, 决定写一个简易版本; 忙着做更多题, 没有时间多考虑优化代码, 所以, 就写一个试试运气. http://blog.csdn.net/kenden23/article/details ...

随机推荐

  1. ES2015 ——let命令的暂时性死区

    ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. 和var不同的还有,let命令不存在变量提升,所以声明前调用变量,都会报错,这就涉及到 ...

  2. nginx php 安装

    .选定源码目录选定目录 /data/klj/ cd /data/klj/ 2.安装PCRE库cd /data/klj/wget ftp://ftp.csx.cam.ac.uk/pub/software ...

  3. RecyclerView的基本创建

    线性显示 类似于listview: 线性宫格显示 类似于grid view: 用线性宫格显示 类似于瀑布流: 结构图: 测试代码: activity_main.xml: <RelativeLay ...

  4. STM32F0xx_DMA收发USART数据配置详细过程

    前言 关于DMA(Direct Memory Access)的功能,前面关注我微信的人应该知道,其实我已经在F1芯片上简单讲了一下.有网友要求在F0讲解一下使用DMA收发串口数据.今天就应网友要求总结 ...

  5. 4.html5中超链接

    html中超链接都是通过<a>标签实现的,html5也不例外,这里就来探讨一下<a>标签. <a>元素属于文本元素,有一些私有属性或者叫局部属性.那么,相对应的还有 ...

  6. xcode plugin

    http://alcatraz.io/ https://github.com/macoscope/CodePilot prepo  curl -fsSL https://raw.githubuserc ...

  7. Helloworld模块之内核makefile详解

    Hello World 模块以及对应的内核makefile详解 hello.c: #include <linux/module.h> //所有模块都需要的头文件 #include < ...

  8. hdu 1212 Big Number

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Big Number Description As we know, Big Number is ...

  9. 基于opencv 的图片模糊判断代码

    #include"cv.h"  #include"highgui.h"  #include<iostream>  using namespace s ...

  10. discuz分类信息地区联动菜单字段

    1 = 河南省 1.1 = 郑州市 1.1.1 = 中原区 1.1.2 = 二七区 1.1.3 = 管城区 1.1.4 = 金水区 1.1.5 = 上街区 1.1.6 = 惠济区 1.1.7 = 巩义 ...