【一天一道LeetCode】#57. Insert Interval
一天一道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的更多相关文章
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
		
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
 - 第一周 Leetcode  57. Insert Interval (HARD)
		
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
 - 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 ...
 - [LeetCode] 57. Insert Interval 插入区间
		
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
 - LeetCode: 57. Insert Interval(Hard)
		
1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...
 - LeetCode 57. Insert Interval 插入区间 (C++/Java)
		
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
 - Leetcode#57 Insert Interval
		
原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...
 - [LeetCode] 57. Insert Interval 解决思路
		
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
 - [leetcode]57. Insert Interval插入区间
		
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
 - Leetcode 57: Insert Interval 让代码更好读, 更容易测试.
		
阅读了几个博客, 决定写一个简易版本; 忙着做更多题, 没有时间多考虑优化代码, 所以, 就写一个试试运气. http://blog.csdn.net/kenden23/article/details ...
 
随机推荐
- 王家林人工智能AI课程大纲和电子书 - 老师微信13928463918
			
**3980元团购原价19800元的AI课程,团购请加王家林老师微信13928463918. 基于王家林老师独创的人工智能"项目情景投射"学习法,任何IT人员皆可在无需数学和Pyt ...
 - Bootstrap3 栅格系统-栅格参数
			
通过下表可以详细查看 Bootstrap 的栅格系统是如何在多种屏幕设备上工作的. -–下面有个"顶"字,你懂得O(∩_∩)O哈哈~ -–乐于分享,共同进步! -–更多文章请看:h ...
 - Spring Security 基础登录实例
			
1 新建Java Web项目 导入Jar: 2 修改web.xml <?xml version="1.0" encoding="UTF-8"?> & ...
 - Git之(四)分支管理
			
当我们初始化Git仓库的时候,Git会默认创建一个名为master的主分支.在实际工作中,主分支要求是一个稳定.健壮.安全的主线,一般不允许在主分支上直接进行开发,而是拉取一个新的分支,开发.测试完成 ...
 - linux:CPU私有变量(per-CPU变量)
			
一.简介2.6内核上一个新的特性就是per-CPU变量.顾名思义,就是每个处理器上有此变量的一个副本.per-CPU的最大优点就是,对它的访问几乎不需要锁,因为每个CPU都在自己的副本上工作.task ...
 - Docker学习笔记1:CentOS7 下安装Docker
			
本文内容摘自官网:https://docs.docker.com/engine/installation/linux/centos/#/create-a-docker-group 注:本文是介绍Lin ...
 - N个鸡蛋放到M个篮子中
			
N个鸡蛋放到M个篮子中,篮子不能为空,要满足:对任意不大于N的数量,能用若干个篮子中鸡蛋的和表示. 写出函数,对输入整数N和M,输出所有可能的鸡蛋的放法. 比如对于9个鸡蛋5个篮子 解至少有三组: 1 ...
 - python 3.3.3 字面量,正则,反斜杠和原始字符串
			
两个不起眼但是比较重要的设定 Python str类型的字面量解释器 当反斜杠及其紧接字符无法构成一个具有特殊含义的序列('recognized escape sequences')时,Python选 ...
 - Scikit-learn:主要模块和基本使用方法
			
http://blog.csdn.net/pipisorry/article/details/52128222 scikit-learn: Machine Learning in Python.sci ...
 - 统计处理包Statsmodels: statistics in python
			
http://blog.csdn.net/pipisorry/article/details/52227580 Statsmodels Statsmodels is a Python package ...