题目

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. 【PHP】在目标字符串指定位置插入字符串

    PHP如何在指定位置插入相关字符串,例子:123456789变为1_23_456789插入"_"到指定的位置! (可以用作换行或者其他处理) 插入示例,具体思路在代码中有注释: & ...

  2. Sphinx高亮显示关键字

    选取程序中使用的一部分代码: public function buildExcerptRows($ids) { $options = array( 'before_match' => '< ...

  3. webqq协议分析之~~~~验证是否需要验证码

    对于小黄鸡我想大家(喜欢在群里bb的人...)肯定一点都不陌生,那段时间大家在群里对小鸡是各种调戏啊,都有点不忍直视.那时我便想能不能自己也做个呢,后来想想还是算了吧,自己技术太渣渣,然后就不了了之. ...

  4. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第10章节--SP2013中OAuth概览 应用程序验证

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第10章节--SP2013中OAuth概览  应用程序验证 既然你懂得了什么是应用程序身份.而且知道怎样在SP中创建和设置 ...

  5. 用C语言实现循环左移和循环右移

    有天重建一段代码时,遇到了循环右移指令,不知道用C语言怎么实现,后来得到小伟指点,感谢.. me 15:56:38004BD2C9 8B55 F8 MOV EDX,DWORD PTR SS:[EBP- ...

  6. mysql lower_case_table_names ---- 一律把表名处理为小写

    一.从操作系统说起: 1.我们知道mysql 是跨平台的.它可以在许多平台上运行如windows .linux.unix(mac).linux 是类unix的, 但是windows和linux就有非常 ...

  7. ARM开发工具软件命令具体解释---嵌入式回归第三篇

    先从bootloader開始,由于临时眼下这些都会是裸机程序相关. 本人这里是VMwarm10.0上安装的红帽linux虚拟机.从以下的截图中能够看出 裸机开发流程: 这里先做第三步(第一步第二步已提 ...

  8. linux中录屏工具byzanz

    linux中录屏工具byzanz: 1.安装 sudo apt install byzanz 2.使用 help:byzanz-record --help 配合xwininfo使用--xwininfo ...

  9. phoenix系统创建语句

    CREATE TABLE SYSTEM."CATALOG"( TENANT_ID VARCHAR NULL, TABLE_SCHEM VARCHAR NULL, TABLE_NAM ...

  10. 在echarts中自定义提示框内容

    1.期望效果 以柱状图为例,在鼠标滑过每个数据标签时,为了更友好地显示数据内容,需要对显示的数据内容作格式化处理,添加自定义内容. 如下图,鼠标滑过每个数据项时, 第1张是默认提示框: 第2张是处理成 ...