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

思路:开始想用线段树,后来想想这个不是动态变化的没必要。

按区间的第一个值从小到大排序,然后跳过后面被覆盖的区间来找。

sort折腾了好久,开始搞不定只好自己写了一个归并排序。现在代码里的sort是可用的。

class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
//mysort(intervals, 0, intervals.size() - 1);
sort(intervals.begin(), intervals.end(), comp);
vector<Interval> ans;
Interval tmp;
for(int i = ; i < intervals.size(); i++) //找每个连续的区间
{
tmp.start = intervals[i].start;
int curmaxend = intervals[i].end; //注意这里要记录当前区间最大的那个值 因为有可能[1,100],[2,3]这样前面最大值比后面大
while(i + < intervals.size() && curmaxend >= intervals[i + ].start)
{
curmaxend = (intervals[i + ].end > curmaxend) ? intervals[i + ].end : curmaxend;
i++;
}
tmp.end = curmaxend;
ans.push_back(tmp);
}
return ans;
} void mysort(vector<Interval> &intervals, int s, int e)
{
if(s >= e) return; int m = (s + e) / ;
mysort(intervals, s, m);
mysort(intervals, m + , e);
merge(intervals, s, m, e);
} void merge(vector<Interval> &intervals, int s, int m, int e)
{
vector<Interval> left(intervals.begin() + s, intervals.begin() + m + );
vector<Interval> right(intervals.begin() + m + , intervals.begin() + e + ); int l = , r = , n = s;
while(l < left.size() && r < right.size())
{
intervals[n++] = (left[l].start < right[r].start) ? left[l++] : right[r++];
}
while(l < left.size())
{
intervals[n++] = left[l++];
}
while(r < right.size())
{
intervals[n++] = right[r++];
}
} static bool comp(const Interval& a, const Interval& b){
return a.start < b.start;
}
};

【leetcode】Merge Intervals(hard)的更多相关文章

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

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

  2. 【leetcode】Merge Intervals

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

  3. 【leetcode】 Merge Intervals

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

  4. LeetCode: 56. Merge Intervals(Medium)

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

  5. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  6. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  7. 【leetcode】Reorder List (middle)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  8. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  9. 【leetcode】sort list(python)

    链表的归并排序 超时的代码 class Solution: def merge(self, head1, head2): if head1 == None: return head2 if head2 ...

随机推荐

  1. jQuery1.9.1源码分析--数据缓存Data模块

    jQuery1.9.1源码分析--数据缓存Data模块 阅读目录 jQuery API中Data的基本使用方法介绍 jQuery.acceptData(elem)源码分析 jQuery.data(el ...

  2. zookeeper集群配置与启动——实战

    1,准备: A:三台linxu服务器: 10.112.29.177 10.112.29.172 10.112.29.174 命令 hostname 得到每台机器的 hostname vm-10-112 ...

  3. PHP基础 mysqli的事务处理

    1: <?php 2: // PHP 的mysqli的事务处理 3: //======================================================== 4: ...

  4. php连接mysql

    一.php连接mysql的函数 1.mysql_connect 作用:连接mysql eg:$con=mysql_connect('localhost','root','123456'); 2.mys ...

  5. 【bzoj2243】[SDOI2011]染色

    题目描述 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如"11 ...

  6. centos 6.5 zabbix3.0.4 监控apache

    开启apache的server-status httpd.conf 末尾添加 [root@test3 /]# vim /usr/local/httpd-/conf/httpd.conf Extende ...

  7. 演示Android百度地图操作功能

    在本文中将演示百度地图的操作功能,包括缩放,旋转,视角切换,点击,双击,长按事件触发的操作以及截图等.百度地图本来就内置有缩放,旋转功能,那么在这里,截图(其实很多手机也自带截图功能)以及点击事件的监 ...

  8. iOS企业级开发初级课程-表视图(13集)

    首先了解了表视图的组成.表视图类的构成.表视图的分类以及表视图的两个重要协议(委托协议和数据源协议),对表视图有了一个整体上的认识.接下来我们掌握了如何实现简单表视图和分节表视图,以及表视图中索引.搜 ...

  9. HDU 5053 the Sum of Cube

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5053 解题报告:用来陶冶情操的题,求a到b的三次方的和. #include<stdio.h> ...

  10. 为在韶大痛苦而不能用手机、Pad等上网的同志造福!

    目标:共享咱们校园网,让更多的人或更多的设备冲浪去! 基本条件:一台带无线功能的笔记本,一个可以上网的账号与pwd,最好为Windows7以上的操作系统,如果是XP,则需要打个.net framewo ...