给出一个区间的集合, 请合并所有重叠的区间。
示例:
给出 [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. JavaScript中函数的无限次运算问题

    开博客有一段时间了,一直没动笔,原因是确实没看到什么值得写的内容.直到今天在司徒正美的博客里看到一个问题. http://www.cnblogs.com/rubylouvre/archive/2012 ...

  2. Mybatis异常_03_Invalid bound statement (not found)

    一.异常信息 Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...

  3. listen 59

    Different Brain Regions Handle Different Music Types (Vivaldi) versus (the Beatles) . Both great. Bu ...

  4. Python: scikit-image binary descriptor

    这个用例说明 BRIEF binary description algorithm from skimage import data from skimage import transform as ...

  5. Mac使用记录

    ---恢复内容开始--- brew list //查看brew安装东东 ls //当前目录下内容 brew --cache //查看brew下载目录 /usr/local/Cellar/ //隐藏文件 ...

  6. GIT的Push和Pull,强制Pull覆盖本地命令

    连接命令: git remote add origin + 你Git库的地址 其中,origin是你对这个Git库地址的标识. 一. 把文件从本地上传到库中 第一步:使用命令 git add命令把文件 ...

  7. ACM学习历程—ZOJ3878 Convert QWERTY to Dvorak(Hash && 模拟)

    Description Edward, a poor copy typist, is a user of the Dvorak Layout. But now he has only a QWERTY ...

  8. hdu畅通工程

    传送门 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  9. P1417烹调方案——背包问题中的排序

    题目:https://www.luogu.org/problemnew/show/P1417 与普通的01背包不同的一点是加入物品的顺序对结果有影响,这里可以考虑贪心的想法,把对全局影响最小的物品排在 ...

  10. Swing 添加Esc快捷键退出程序

    JFrame demo = new JFrame(); demo.addKeyListener( new KeyListener(){ public void keyReleased(KeyEvent ...