题目

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

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

 
思路:
1. 模拟题
2. Insert Interval 是 Merge Interval 的扩展, 将 newInterval 插入到 Intervals 即可转化成 Merge Interval
 
代码
bool sortInterval(const Interval &i1, const Interval &i2) {
return i1.start < i2.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> newVec;
if(intervals.size() <= 0)
return newVec; sort(intervals.begin(), intervals.end(), sortInterval);
for(int i = 0; i < intervals.size(); i ++) {
Interval nextInt = intervals[i];
if(i == intervals.size()-1) {
newVec.push_back(nextInt);
break;
}
Interval tmp = intervals[i+1];
while(tmp.start <= nextInt.end) {
nextInt.end = max(tmp.end, nextInt.end);
i++;
if(i+1<intervals.size())
tmp = intervals[i+1];
else {
newVec.push_back(nextInt);
return newVec;
}
}
newVec.push_back(nextInt);
}
return newVec;
}
};

  

Leetcode: Merge/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 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 ...

  3. 【leetcode】Insert Interval

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

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

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

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

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

  6. Java for LeetCode 057 Insert Interval

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

  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 插入区间 (C++/Java)

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

  9. [Leetcode][JAVA] Insert Interval

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

随机推荐

  1. 【phpstorm】破解安装

    1.使用前修改C:\windows\system32\Driver\hosts文件,将“0.0.0.0 account.jetbrains.com”添加到hosts文件中. 2. 浏览器打开 http ...

  2. 层次选择器[selector_2.html]

    层次选择器[selector_2.html] $("form input"):祖先 后代 $("form>input"):父亲>直接小孩 $(&qu ...

  3. HttpSolrServer 实例管理参考,来自org.eclipse.smila.solr

    http://dev.eclipse.org/svnroot/rt/org.eclipse.smila/trunk/core/org.eclipse.smila.solr/code/为什么要对实例管理 ...

  4. Android中 单位 介绍

    看到有很多网友不太理解dp.sp和px的区别:现在这里介绍一下dp和sp.dp也就是dip.这个和sp基本类似.如果设置表示长度.高度等属性时可以使用dp 或sp.但如果设置字体,需要使用sp.dp是 ...

  5. python --正则学习

    re的正则表达式语法    正则表达式语法表如下: re.match re.match 尝试从字符串的开始匹配一个模式,匹配成功返回match object,否则返回None. 如:下面的例子匹配第一 ...

  6. mfc获取控件在对话框上的位置

    CRect rect; GetDlgItem(控件ID)->GetWindowRect(&rect);//获取控件的屏幕坐标ScreenToClient(&rect);//转换为 ...

  7. Jquery使容器自适应浏览器窗口

    一.几个关键点 1:当文档大小改变时可以通过哪个事件来触发? resize([Data], fn) 可传入data供函数fn处理. 示例: $(window).resize(function(){ a ...

  8. chrome 浏览器插件开发

    一.chrome 浏览器插件开发是什么: 1 从技术上说插件只是一个存在于本地的一个网站.所以呢在插件开发的过程中用到的技术无非是 javascript .html .css . 二.把当前活动页面的 ...

  9. 如何编写安全的PHP代码

    转于:http://www.nowamagic.net/php/php_HowToWriteSafePhpCode.php

  10. beyond compare比较工具设置

    beyond compare用于比较的工具,云盘:比较   链接: https://pan.baidu.com/s/1boZbB0F