Given a collection of intervals, merge all overlapping intervals.

 
Example

Given intervals => merged intervals:

[                     [
[1, 3], [1, 6],
[2, 6], => [8, 10],
[8, 10], [15, 18]
[15, 18] ]
]
Challenge

O(n log n) time and O(1) extra space.

解法一:

 /**
* Definition of Interval:
* classs Interval {
* int start, end;
* Interval(int start, int end) {
* this->start = start;
* this->end = end;
* }
*/ class Solution {
public:
/**
* @param intervals: interval list.
* @return: A new interval list.
*/
static bool cmp(const Interval &a, const Interval &b) {
return (a.start < b.start);
} vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> ans;
if (intervals.empty()) {
return ans;
} sort(intervals.begin(), intervals.end(), cmp);
ans.push_back(intervals[]);
for (int i = ; i < intervals.size(); i++) {
if (ans.back().end >= intervals[i].start) {
ans.back().end = max(ans.back().end, intervals[i].end);
} else {
ans.push_back(intervals[i]);
}
}
return ans;
}
};

先排序,然后逐个判断是否需要合并

156. Merge Intervals【easy】的更多相关文章

  1. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  2. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  3. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  4. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  5. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  6. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  7. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  8. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  9. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

随机推荐

  1. Android记一次后台保活设计心得2018

    首先我并不推荐也不喜欢手机应用通过各种手段后台保活,但是当产品经理确定一定以及肯定地提出了这个需求,活着应用有着必须常驻后台的理由,也就只有硬着头皮去与各个手机的后台管理机制做斗争了. 背景:因为开发 ...

  2. JS跳转页面的几种方法

    JS的几种跳转方式: 1. window.open(”url“) 2.用自定义函数 <script> function openWin(tag,obj) { obj.target=&quo ...

  3. 为什么深度神经网络难以训练Why are deep neural networks hard to train?

    Imagine you're an engineer who has been asked to design a computer from scratch. One day you're work ...

  4. python 机器学习中的数据处理学习记录

    在机器学习中,选择合适的算法固然重要,但是数据的处理也同样重要.通过对数据的处理,能提高计算效率,提高预测识别精确度等等 以下记录下一些数据处理的方法 一.处理缺失值 对于数据集中有缺失值的,粗暴的方 ...

  5. python 如何调用子文件下的模块

    在python开发中,经常会出现调用子文件夹下的py模块 如上图,如果在test.py文件中,要调用meeting文件夹下面的huodongshu.py 模块, 直接在test.py 中 import ...

  6. scala sbt 添加国内镜像

    FROM: http://www.4wei.cn/archives/1002417 sbt运行时经常需要下载大量的jar包,默认连接到maven官网,速度通常比较慢.在`~/.sbt/`下添加一个`r ...

  7. javascript 中用到的时间戳函数

    JavaScript 获取当前时间戳:第一种方法: var timestamp = Date.parse(new Date()); 例如结果:1280977330000第二种方法: var times ...

  8. java线程总结(3/5)

    一.线程同步和死锁问题 异步问题: package com.horizon.action; /** * 测试同步问题 * */ public class TestSync { public stati ...

  9. 【转】myeclipse中连接mysql数据库

    1. 环境配置 下载地址:   http://www.mysql.com/downloads/mysql/ 真麻烦,下载的话还需要注册和登录以及填个表.上面的信息还挺全的,乱填的信息也是可以接受的~~ ...

  10. 算法笔记_061:蓝桥杯练习 字串统计(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 给定一个长度为n的字符串S,还有一个数字L,统计长度大于等于L的出现次数最多的子串(不同的出现可以相交),如果有多个,输出最长的,如果仍然 ...