题目:给定一连串的区间,要求输出不重叠的区间。

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

这题主要会要知道如何对一个node型或者是struct进行排序。就是如何定义sort的比较函数,sort的比较函数本来默认的是小于函数,现在给定的struct类型没有定义的小于函数,所以要自己定义一个。根据左区间排序。

定义好后,进行排序,排好序后,扫面一次判断下一个的开头和前一个的结尾的大小,如果下一个开头大,那么前面一个就是分离的区间,记录答案。如果下一个的开头不比上一个的结尾大,那么就有重叠了,这时候就判断下一个的结尾是否比前一个的结尾大,大的话就替代它。所以我们要有一个指针指向保存了答案的vector的末尾,以便于和下一个开头判断。

/**
* 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 {
private:
static bool myCmp(Interval a, Interval b)
{
return a.start < b.start;
}
public:
vector<Interval> merge(vector<Interval> &intervals)
{
if(intervals.empty()) return intervals;
sort(intervals.begin(), intervals.end(), myCmp);
vector<Interval> ans;
int cnt = ;
while(cnt < intervals.size() )
{
Interval tmp;
tmp = intervals[cnt];
while(cnt + < intervals.size() && intervals[cnt + ].start <= tmp.end )
{
if (intervals[cnt + ].end >= tmp.end)
{tmp.end = intervals[cnt + ].end;cnt++;}
else cnt++; // 排除下一个区间已经被上一个区间包围的例子
}
cnt++;// 记得要再加一,从下一个开始
ans.push_back(tmp);
}
return ans;
}
};

更详细请参见这位

/**
* 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 {
private:
static bool comp(Interval a, Interval b)
{
return a.start < b.start;
}
public:
vector<Interval> merge(vector<Interval> &intervals) {
if(intervals.empty())return intervals;
sort(intervals.begin(), intervals.end(), comp);
vector<Interval> res;
res.push_back(intervals[]);
for(int i = ; i < intervals.size(); i++)
{
Interval &p = res.back(); // 找到末尾,为了和下一个开头比较
if(intervals[i].start > p.end)res.push_back(intervals[i]);
else if(intervals[i].end > p.end)p.end = intervals[i].end;
}
return res;
}
};

leetcode[55] Merge Intervals的更多相关文章

  1. [Leetcode Week2]Merge Intervals

    Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...

  2. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

  3. 【leetcode】Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  4. 【leetcode】Merge Intervals(hard)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  5. 【leetcode】 Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  6. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  7. LeetCode 56. Merge Intervals (合并区间)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  8. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  9. Java for LeetCode 056 Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

随机推荐

  1. HTML与XHTML差额

    实际上.XHTML 与 HTML 4.01 标准没有太多的不同. 它们最基本的不同.举例说明例如以下: 1.XHTML 元素必须被正确地嵌套. 错误:<p><span>this ...

  2. Scut游戏server引擎Unity3d访问

    Scut提供Unity3d Sdk包.便利的高速发展和Scut游戏server对接: 看Unity3d示为以下的比率: 启动Unity3d项目 打开Scutc.svn\SDK\Unity3d\Asse ...

  3. 于PsIsSystemThread无论是在线程系统线程标识获得

    我一直好奇一个进程的所有线程改变线程标志Terminated mov edi, edi ; IoIsSystemThread push ebp mov ebp, esp mov eax, [ebp+a ...

  4. Codeforces 439C Devu and Partitioning of the Array(模拟)

    题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,可是仅仅须要关注该份的和为奇数还是偶数 ...

  5. lua转让C++书面DLL达到“热更新”

    原创作品,请注明出处转载CSDN:http://blog.csdn.net/relar/article/details/38084689 开发游戏server往往有"热更新"的需求 ...

  6. XP下类似%windir% %userprofile% 的变量的说明(转)

    在一些批处理或者系统技巧操作教程文章中,我们常常会看到一些形如 %windir% 或者 %systemdrive% 的变量.这些变量都代表着什么含义呢?下面小技巧之家为大家整理了在Windows XP ...

  7. 怪异php 语法, 求解!

    查找php馍用来推断是否串串返回值和方法 strpos很奇怪. 请看下面的语句: echo "A1: ".(strpos("csd","c" ...

  8. hdu 5066 Harry And Physical Teacher(Bestcoder Round #14)

    Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  9. 它们的定义ListView,实现Item除去滑动和滑出菜单效果

    这个程序是基于变化从网上开源项目,详情货源忘记.懒得去搜索,.假设有不合适的地方.请与我联系作者.我会及时回复和处理! 序中主要包括两个ListView,一个是实现側滑删除.一个是側滑出菜单,代码中的 ...

  10. React.js初探(一)

    前端框架多如牛毛的今天,团队的技术选型很重要,没有最好的,只有最合适的,这话早已经被说烂了. 但是作为一个有追求的前端,对新技术的敏感以及尝试心理还是要有的. 虽然React已经火的不行了,但由于自己 ...