给出一个区间的集合, 请合并所有重叠的区间。
示例:
给出 [1,3],[2,6],[8,10],[15,18],
返回 [1,6],[8,10],[15,18].
详见:https://leetcode.com/problems/merge-intervals/description/

Java实现:

/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
class Solution {
public List<Interval> merge(List<Interval> intervals) {
List<Interval> res=new LinkedList<Interval>();
int size=intervals.size();
if(intervals==null||size==0){
return res;
}
Collections.sort(intervals,new Comparator<Interval>(){
@Override
public int compare(Interval o1,Interval o2){
if(o1.start<o2.start){
return -1;
}else if(o1.start>o2.start){
return 1;
}else{
return 0;
}
}
});
res.add(intervals.get(0));
for(int i=1;i<size;++i){
Interval cur=intervals.get(i);
Interval top=res.get(res.size()-1);
if(top.end>=cur.start){
top.end=top.end>cur.end?top.end:cur.end;
}else{
res.add(cur);
}
}
return res;
}
}

参考:https://blog.csdn.net/zdavb/article/details/47252657

056 Merge Intervals 合并区间的更多相关文章

  1. [LeetCode] Merge Intervals 合并区间

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

  2. [LeetCode] 56 - Merge Intervals 合并区间

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

  3. Leetcode56. Merge Intervals合并区间

    给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...

  4. LeetCode 56. Merge Intervals 合并区间 (C++/Java)

    题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...

  5. 【LeetCode每天一题】Merge Intervals(合并区间)

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  6. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

  7. [leetcode]56. Merge Intervals归并区间

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  8. Java for LeetCode 056 Merge Intervals

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

  9. merge intervals(合并间隔)

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

随机推荐

  1. 三款功能强大代码比较工具Beyond compare、DiffMerge、WinMerge

    我们经常会遇到需要比较同一文件的不同版本,特别是代码文件.如果人工去对比查看,势必费时实力还会出现纰漏和错误,因此我们需要借助一些代码比较的工具来自动完成这些工作.这里介绍3款比较流行且功能强大的工具 ...

  2. hdu-5793 A Boring Question(二项式定理)

    题目链接: A Boring Question Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java ...

  3. js图片上传及显示

    html部分: <form action='' method='post' name='myform'> <input type='file' id='iptfileupload' ...

  4. Python: scikit-image Blob detection

    这个用例主要介绍利用三种算法对含有blob的图像进行检测,blob 或者叫斑点,就是在一幅图像上,暗背景上的亮区域,或者亮背景上的暗区域,都可以称为blob.主要利用blob与背景之间的对比度来进行检 ...

  5. HihoCoder1670 : 比赛日程安排([Offer收割]编程练习赛41)(模拟)

    描述 H国编程联赛中有N只队伍,编号1~N. 他们计划在2018年一共进行M场一(队)对一(队)的比赛. 为了让参赛队员能得到充分的休息,联赛组委会决定:每支队伍连续两场比赛之间至少间隔一天.也就是如 ...

  6. JXL 的API

    使用Windows操作系统的朋友对Excel(电子表格)一定不会陌生,但是要使用Java语言来操纵Excel文件并不是一件容易的事.在Web应用日益盛行的今天,通过Web来操作Excel文件的需求越来 ...

  7. P2936(BZOJ3396) [USACO09JAN]全流Total Flow[最大流]

    题 裸题不多说,在网络流的练习题里,你甚至可以使用暴力. #include<bits/stdc++.h> using namespace std; typedef long long ll ...

  8. 千万别用MongoDB?真的吗?!

    某人发了一篇Don’t use MongoDB的血泪控诉,我把原文翻译如下,你可以看看.不过,我想我们还要去看看10gen CTO的对此事的回复,我们还要去在Reddit上看看大家的说法,10gen  ...

  9. POJ3580:SuperMemo

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...

  10. 洛谷P2014——选课

    题目:https://www.luogu.org/problemnew/show/P2014 树状DP,注意枚举当前子树中选几个时的边界. 代码如下: #include<iostream> ...