使用最简单的排序方法;

 /**
* 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. wp 修改 提高youtu 速度

    resolve = 后添加 |.googlevideo.com ,并修改 crlf_rules crlf_rules = /^https?:\/\/[^\/]+\.c\.youtube\.com\// ...

  2. ipad屏幕旋转后的代理

    - (void)statusBarOrientationChange:(NSNotification *)notification {         UIInterfaceOrientation o ...

  3. iOS 正则表达式-判断邮箱、手机号

    判断是否是邮箱 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[ ...

  4. 2014-11-26----css的简介

    CSS :层叠样式表 cascading style sheets 它的作用是:美化html网页 格式:样式名:值:样式名:值:样式名:值: 注释语法:/* 注释内容 */ 选中代码按TAB,代码左移 ...

  5. php验证码封装

    /** * 生成验证码 * @param int $type 1: 纯数字,2:纯字母,3:数字与字母混合  * @param int $length * @return string */funct ...

  6. java 反射取得方法入参类型的泛形

    package TestReflectClass; import java.util.List; /** * Created by wangyang on 2016/12/16. */ public ...

  7. PHP中的设计模式:单例模式(译)

    原文链接:http://coderoncode.com/2014/01/27/design-patterns-php-singletons.html 单例模式用于限制类实例化到单个对象,当整个系统只需 ...

  8. MVVM模式应用 之xml文件的读取

    XML如下所示: <?xml version="1.0" encoding="utf-8" ?> <schools> <schoo ...

  9. javascript 学习笔记之JQuery中的Deferred对象

    Deffered是Jquery中的一个非常重要的对象,从1.5版本之后,Jquery中的ajax操作都基于Deffered进行了重构,这个对象的处理模式就像其他Javascript框中的Promise ...

  10. Apache 禁止访问目录

    1.打开apache配置文件httpd.conf 2.找到 <Directory /> Options Indexes AllowOverride None Order allow,den ...