https://oj.leetcode.com/problems/merge-intervals/

合并区间

//排序
sort(intervals.begin(),intervals.end(),CMPFUN);
bool CMPFUN(Interval a, Interval b)
{
return a.start<b.start;
}
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/ bool CMPFUN(Interval a, Interval b)
{
return a.start<b.start;
} class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> ans;
if(intervals.size() == )
return ans; //排序
sort(intervals.begin(),intervals.end(),CMPFUN); size_t index = ; while(index < intervals.size())
{
int start = intervals[index].start;
int end = intervals[index].end;
while((index + < intervals.size()) && end >= intervals[index + ].start)
{
if(end < intervals[index + ].end)
end = intervals[index + ].end;
index++;
}
intervals[index].start = start;
intervals[index].end = end;
ans.push_back(intervals[index]);
index++;
}
return ans;
}
};

LeetCode OJ--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, ...

  10. Leetcode#56 Merge Intervals

    原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...

随机推荐

  1. 科学计算库Numpy——运算

    np.multiply(array1,array2) 该函数用于数组中对应位置上的数相乘. 一维向量 二维数组 np.dot(array1,array2) 两个数组都是一维向量 数组中对应位置上的数相 ...

  2. pip3 的安装 同时安装lxml和pygame

    ubuntu18.04中 首先查看自己电脑的python版本,一般都会有2, 和3 python -V python3 -V 查看pip版本 pip -V pip3 -V 现在我们就可以开始安装我们的 ...

  3. [Hdu3507]Print Article(斜率优化)

    Description 题意:给N个数,按顺序全部取走,每次取一段连续的区间,代价为\((S[i]-S[j])^2+M\) 其中M为一个给定的常数,\(S[i]\)为前缀和 \(N\leq 50000 ...

  4. CodeForces 781D Axel and Marston in Bitland DP

    题意: 有一个\(n\)个点\(m\)条边的无向图,边有两种类型,分别用\(0\)和\(1\)标识 因此图中的任意一条路径都对应一个\(01\)字符串 定义一个无限长的字符串\(s\): 开始令\(s ...

  5. 记一次开发过程中,iview遇到的一些坑以及解决办法

    写在开头:本次项目采用的是vue2.0+iview3.0,最近公司没啥事,来总结一下开发过程中遇到的问题. 1.Modal关闭问题 需求背景:modal框里面是个form表单,点击确定之后,先验证fo ...

  6. synchronized同步方法和同步代码块的区别

    同步方法默认使用this或者当前类做为锁. 同步代码块可以选择以什么来加锁,比同步方法更精确,我们可以选择只有会在同步发生同步问题的代码加锁,而并不是整个方法. 同步方法使用synchronized修 ...

  7. 设计模式之第20章-访问者模式(Java实现)

    设计模式之第20章-访问者模式(Java实现) “嘿,你脸好红啊.”“精神焕发.”“怎么又黄了?”“怕冷,涂的,涂的,蜡.”“身上还有酒味,露馅了吧,原来是喝酒喝的啊.”“嘿嘿,让,让你发现了,今天来 ...

  8. IOS开发---菜鸟学习之路--(二十三)-直接利用键值对的方式来处理数据的感想

    首先声明,本文纯粹只是做为本人个人新手的理解.文中的想法我知道肯定有很多地方是错的. 但是这就是我作为一个新人的使用方法,对于大牛非常欢迎指导,对于喷子请绕道而行. 由于这是早上跟我学长讨论数据处理时 ...

  9. Monkey日志信息的11种Event percentages

    我们查看官方文档,表里只给出了8种事件(可以看我上篇的翻译文档).但我们运行Monkey后,却发现有11种事件!最坑爹的是,在每种事件的百分比后面,他还不给注明是什么事件! 原来不同的Android ...

  10. csu-2018年11月月赛Round2-div2题解

    csu-2018年11月月赛Round2-div2题解 A(2193):昆虫繁殖 Description 科学家在热带森林中发现了一种特殊的昆虫,这种昆虫的繁殖能力很强.每对成虫过x个月产y对卵,每对 ...