Merge Intervals

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

 
 
先排序,然后循环合并
 
 /**
* 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 {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> result;
if(intervals.empty()) return result; sort(intervals.begin(),intervals.end(),cmp); int start=intervals[].start;
int end=intervals[].end; for(int i=;i<intervals.size();i++)
{
if(intervals[i].start<=end)
{
//注意[1,4],[2,3]的情况
end=end<intervals[i].end?intervals[i].end:end;
}
else
{
result.push_back(Interval(start,end));
start=intervals[i].start;
end=intervals[i].end;
}
} result.push_back(Interval(start,end));
return result;
} static int cmp(const Interval &a,const Interval &b)
{
if(a.start<b.start) return ;
else if(a.start>b.start) return ;
else return a.end<b.end;
}
};

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

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

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

  2. 【leetcode】Merge Intervals(hard)

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

  3. 【leetcode】 Merge Intervals

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

  4. 【Leetcode】【Hard】Merge Intervals

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

  5. 【LeetCode】Merge Sorted Array(合并两个有序数组)

    这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nu ...

  6. 【leetcode】Merge Sorted Array

    题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...

  7. 【leetcode】Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  8. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  9. 【leetcode】Merge Two Sorted Lists(easy)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

随机推荐

  1. js获取iframe里的body内容

    做个页面 需要加入a.html 使用的js动态添加iframe 直接JQ添加的 代码 $(".banner-box").after(“<iframe src="ht ...

  2. Working with Transactions (EF6 Onwards)

    Data Developer Center > Learn > Entity Framework > Get Started > Working with Transactio ...

  3. aop测试jdk代理机制

    //测试jdk代理机制 @Test public void testProxy(){ final UsbDisk usbDisk = new UsbDisk(); //类加载器,接口,匿名内部类 // ...

  4. JavaScript 五种(非构造方式)继承

    参考链接:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html

  5. Xcode 历史版本

    概述[编辑] Xcode前身是继承自NeXT的Project Builder. The Xcode suite包含有GNU Compiler Collection自由软件(GCC.apple-darw ...

  6. 导出Excel之Epplus使用教程1(基本介绍)

    1.前言 目前Epplus的介绍中文资料很少,我也一直在摸索中使用它,以下是我在使用过程中得到的经验,写出来供大家参考.本系列共4章: 导出Excel之Epplus使用教程1(基本介绍) 导出Exce ...

  7. gtest

    一.安装配置 1.简介 2.安装 下载地址: https://code.google.com/p/googletest/downloads/list 解压安装: unzip gtest-1.7.0.z ...

  8. HTML5+学习笔记1-------边看代码边研究中

    document.addEventListener('touchstart',function(){ return false; },true); touchstart当手指触摸屏幕时候触发,即使已经 ...

  9. IE打开报错,提示该内存不能为read的解决办法!

    由于最近我遇到过一次浏览器打不开的情况,出错的错误提示为 浏览器错误:“0x5ddfddac”指令引用的“0x00000020”内存,该内存不能为read经过杀毒及IE修复均不能解决!(没试过360急 ...

  10. PHP的 Mysqli扩展库的多语句执行

    $mysqli->multi_query($sqls);     执行多个sql语句,返回true/false 有结果集时,使用 $mysqli->store_result(); 来获取结 ...