一天一道LeetCode系列

(一)题目

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

(二)解题

相关题目:【一天一道LeetCode】#56. Merge Intervals


/*

主要解题思路:

1、当intervals[i].end<newInterval.start的时候,不需要合并,直接i++

2、当找到第一个满足intervals[i].end>newInterval.start的时候,确定合并后的interval的start值

3、当找到最后一个满足intervals[i].start>newInterval.end的时候,确定合并后的interval的end值

*/

/**

 * 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) {

        vector<Interval> ret;

        if(intervals.size()==0){//特殊情况

            ret.push_back(newInterval);

            return ret;

        }

        int i = 0;

        Interval tmp;

        while(i<intervals.size()&&intervals[i].end<newInterval.start) {//将不需要合并的Interval直接压入ret

            ret.push_back(intervals[i]);

            i++;

        }

        //找到了第一个满足intervals[i].end>newInterval.start的i值

        //这里需要注意当newInterval比vector里面的值都大的情况

        tmp.start = min(i==intervals.size()?newInterval.start:intervals[i].start,newInterval.start);

        tmp.end = newInterval.end;

        while(i<intervals.size()&&intervals[i].start<=newInterval.end)

        {

            tmp.end = max(intervals[i].end,newInterval.end);//找到合并后的end值

            i++;

        }

        ret.push_back(tmp);

        while(i<intervals.size()) {//将剩下的Interval都压入ret

            ret.push_back(intervals[i]);

            i++;

        }

        return ret;

    }

};

【一天一道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

    原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...

  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插入区间

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

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

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

随机推荐

  1. webpack4.1.1的使用详细教程

    安装全局webpack cnpm install -g webpack 安装全局webpack-cli npm install -g webpack-cli 初始化:生成package.json文件 ...

  2. springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)

    在上一篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)我们介绍了返回值解析器HandlerMethodReturnValueHand ...

  3. Unity发布至IOS的流程(踩坑记录)

    这篇文章主要用于记录本人亲身经历过的Unity发布到IOS平台所遇到的所有坑(其实也就是一些自己并不明白的强制设定),以便于后续再有类似需求时少走些弯路. 我的环境: Unity 5.2.2 个人版( ...

  4. postgresql添加字段

    ALTER TABLE jiangan_config ADD COLUMN article text NOT NULL DEFAULT ''; pg_dump -s database_name -t ...

  5. WCF Restful调用跨域解决方案

    目前很多项目中CRM更多扮演一个纯后台管理系统,用户更多的操作是在移动端执行,不管是安卓还是IOS甚至是H5.这里以H5为例,CRM提供数据接口,移动web端来调用接口进行数据处理,这里就会涉及到一个 ...

  6. maven项目管理

    systemPath方式 有些不通用的包,maven仓库没有,只能通过本地包依赖,就像下面方式: 在需要依赖的项目建lib文件夹,如下: 然后在pom.xml项目管理文件里面加入本地依赖,如下 这种情 ...

  7. 19 Handler 总结

    Handler 一, 回顾异步任务 AsyncTask 二, android 使用线程的规则 1,在主线程 不能做阻塞操作 2,在主线程之外的线程不能更新Ui 三, Handler的作用 1,在子线程 ...

  8. cassandra 如何写数据以及放置副本

    application发送数据到server application 发送请求到server 根据设置的load balance 规则从cluster中挑选一个coordinator,一般使用轮询即可 ...

  9. Spring Resource接口获取资源

    1.1.1. Resource简介 在Spring内部实现机制,针对于资源文件(配置的xml文件)有一个统一的接口Resource. 1.1.1.1. 接口定义的方法 1.exists():判断资源文 ...

  10. Oracle 大数据集成实施

    Oracle 大数据实施架构 Oracle为广大客户提供了一个预装的用于测试和学习目的的免费大数据环境.你可以在这个环境中对Oracle大数据一体机(Big Data Appliance)上的可选软件 ...