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. jQuery Ajax 参数解析

    简单的例子: $.ajax({ type:"post", data:{a:acon,b:bcon} , url:"ajax.php", async:false ...

  2. Oracle数据库查看用户状态

    一.当前ORACLE用户的状态可查看视图DBA_USERS;一般情况下在使用的正常用户均处于OPEN状态. 1 SQL> select username,account_status from  ...

  3. 配置安全证书的Etcd集群

    不知在哪篇技术文档中看到,kubernetes master和etcd分开部署模式,因为集群的状态都保存在etcd中,这样当kubernetes master挂掉后,通过API Server交互的Sc ...

  4. Linux中线程使用详解

    线程与进程为什么有了进程的概念后,还要再引入线程呢?使用多线程到底有哪些好处?什么的系统应该选用多线程?我们首先必须回答这些问题. 使用多线程的理由之一是和进程相比,它是一种非常"节俭&qu ...

  5. android 开源项目集合

    http://p.codekk.com/ http://www.apkbus.com/code.php http://androidxref.com/ https://www.androidos.ne ...

  6. needtrue需要真实的答案

    现在从底层做起来,相当的不容易啊,无论是哪个行业,每个人都需要付出很多的努力.但是现在人们的心是浮躁的,都想一下得到自己想要的东西,钱也好,车也好,房子也好,女人也好.最近很喜欢两句话,这里写下来与大 ...

  7. C#秘密武器之特性

    一.概述 Attribute说白了就是一个类而已,里边一般含有一些附加信息,或者一些特殊的处理逻辑,以便告诉编译器应用该特性的东东是个奇葩,需要特殊对待! 二.使用时的注意事项 2.1. Attrib ...

  8. WebService 之 工作原理

    一.Web Service基本概念 Web Service 也叫 XML Web Service,WebService 是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求 ...

  9. Mybatis <where>标签

    <select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHER ...

  10. Debian9.3安装NW360无线网卡驱动

    最近想把家里的一台老旧台式机利用起来,打算安装Debian9.3,下载ISO,用PowerISO写入到U盘,然后开始安装,过程基本顺利. 就是中间提示缺少“rtl_nic/rtl8105e-1.fw” ...