原题地址

遍历每个区间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. Yii中使用PHPexcel获取excel中数据

    1.view中代码如下: <form name="frmBatchSettle" id="" action="" method=&qu ...

  2. 【转】Spark快速入门指南

    尊重版权,原文:http://blog.csdn.net/macyang/article/details/7100523   - Spark是什么? Spark is a MapReduce-like ...

  3. 雷达装置 (POJ 1328/ codevs 2625)题解

    [问题描述] 假定海岸线是一条无限延伸的直线,陆地在海岸线的一边,大海在另一侧.海中有许多岛屿,每一个小岛我们可以认为是一个点.现在要在海岸线上安装雷达,雷达的覆盖范围是d,也就是说大海中一个小岛能被 ...

  4. Form认证导致登陆页面的样式无效和图片不能显示的原因

    最近在做企业内门户网站,一切进展还算顺利,部署到生产环境的时候也能没有什么大问题,只是登录页面的样式不起作用,不知为何,因为是使用了login控件,最初以为是此控件有内置默认样式或者什么原因,于是就不 ...

  5. python Django 学习笔记(一)—— Django安装

    注:本人python版本2.7.5 ,win7系统 安装Django https://www.djangoproject.com/download/ 官方下载Django-1.5.5.tar.gz 1 ...

  6. SRF之权限控制

    框架目前提供url访问.菜单和页面元素的权限控制和数据权限,权限基于角色来分配,1个用户可以属于多个角色,权限项分模块.页面.操作3级别,其中模块.页面用于url和菜单的控制,操作是对页面元素的控制. ...

  7. POJ 2960 S-Nim<博弈>

    链接:http://poj.org/problem?id=2960 #include<stdio.h> #include<string.h> ; ; int SG[N];//S ...

  8. Hive深入浅出

    1.  Hive是什么 1) Hive是什么? 这里引用 Hive wiki 上的介绍: Hive is a data warehouse infrastructure built on top of ...

  9. git的工作流程(分支合并)

    git支持很多种工作流程,我们采用的一般是这样,远程创建一个主分支,本地每人创建功能分支,日常工作流程如下: 去自己的工作分支$ git checkout work 工作.... 提交工作分支的修改$ ...

  10. Linux中printf格式化输出

    printf使用文本或者由空格分隔的参数,我们可以在printf中使用格式化字符串.printf不会写像echo那样自动添加换行符,必须手动添加 =========================== ...