使用最简单的排序方法;

 /**
* 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; }
* }
*/
public class Solution {
public List<Interval> merge(List<Interval> intervals) {
if(intervals.size()<=1) return intervals;
ArrayList<Interval> arry=new ArrayList<Interval>();
Comparator<Interval> cmp=new Comparator<Interval>(){
public int compare(Interval v1,Interval v2)
{
return v1.start-v2.start;
} };
Collections.sort(intervals,cmp);
Interval pre=intervals.get(0);
for(int i=1;i<intervals.size();i++)
{
Interval cur=intervals.get(i);
if(cur.start<=pre.end)
{
pre=new Interval(pre.start,Math.max(cur.end,pre.end));//merge }
else
{
arry.add(pre);
pre=cur;
} }
arry.add(pre);
return arry; }
}

leetcode 合并区间的更多相关文章

  1. leetcode合并区间

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

  2. [LeetCode] Merge Intervals 合并区间

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

  3. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  4. LeetCode(56):合并区间

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

  5. LeetCode 56. 合并区间(Merge Intervals)

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

  6. Java实现 LeetCode 56 合并区间

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

  7. 力扣leetcode 56. 合并区间

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

  8. leetcode刷题-56合并区间

    题目 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]]输出: [[1,6],[8,10],[15,18]] 思路 通过设置一个移 ...

  9. lintcode:合并区间

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

随机推荐

  1. font awesome icon

    http://fontawesome.io/icons/ http://www.bootstrapicons.com/

  2. Cocos2d-x 学习资料收集

    框架源代码: http://code.google.com/p/cocos2d-x/downloads/list 搭建环境 http://blog.csdn.net/ccf19881030/artic ...

  3. BearSkill实用方法之UITextField限制输入的字符数量

    原文:http://blog.csdn.net/xiongbaoxr/article/details/51525061      

  4. C# 调用AForge类库操作摄像头

    如有雷同,不胜荣幸,若转载,请注明 最近做项目需要操作摄像头,在网上百度了很多资料,很多都是C#调用window API 发送SendMessage,实现操作摄像头,但是C#调用window API的 ...

  5. iterator迭代器的使用

    部分摘自C++ Primer: 所有的标准库容器类都定义了相应的iterator类型,如vector:vector<int>::iterator iter; 这条语句定义了一个名为iter ...

  6. mouseover和mouseout事件在鼠标经过子元素时也会触发

    JavaScript的mouseover和mouseout事件,在绑定元素内部有子元素的情况下, 经过绑定元素时会多次触发mouseover和mouseout事件. jQuery解决办法: jquer ...

  7. JQuery.imgAreaSelect 参数说明

    imgAreaSelect 参数说明: 参数 描述 aspectRatio 设定选取区域的显示比率,如:”4:3“ autoHide 如果设置为true,当选择区域选择结束时消失,默认值为:false ...

  8. spin.js插件的转圈加载效果

    先上插件链接地址:http://fgnass.github.io/spin.js/ 以下是使用spin.js插件的完整版测试例子: <!doctype html> <html> ...

  9. JavaScript 自定义单元测试

    <!doctype html> <html> <head> <meta charset="utf-8"> <script> ...

  10. Xcode6插件开发

    工欲善其事必先利其器,Xcode是我们做iOS Dev必须掌握的一款开发工具. Xcode本身也是一门Cocoa程序,与其来说它是一个Cocoa程序,是不是意味着,我们可以去动态去让它做某件事,或者监 ...