56. Merge Intervals-leetcode-java

【原来在SAE的blog上,都转到CSDN了。。】

56.
Merge Intervals-leetcode-java

发表于 2016/02/08

题意

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

给定一些定义为Interval区间类的List集合,合并有重叠的区间。

思路:先判断整个intervals集合是不是为null或者长度为0或者1,如果是,就直接返回intervals;

否则用collections.sort 对整个intervals集合运用一定的规则排序,这个规则是:根据每个interval的start从小到大排序。

然后定义一个结果集ls,从头到尾遍历整个intervals集合,如果有区间重合,就合并成merged,然后赋值给begin,添加到ls,否则直接分别将两个区间添加到结果集。

这里的排序规则是重新定义了 IntervalComparator实现comparator接口,然后重写了compare方法,这里是返回   return
i1.start - i2.start;,也就是升序排序。

对这句return总感觉没有彻底理解为啥这样是升序呢..先记住吧,略懂不懂的。

/**

* 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==null || intervals.size()<=1) return
intervals;

Collections.sort(intervals,new IntervalComparator());

ArrayList<Interval> ls=new ArrayList<Interval>();

Interval begin=intervals.get(0);

for(int i=1;i<intervals.size();i++){

Interval curr = intervals.get(i);

if(begin.end>=curr.start){

Interval merged=new Interval(begin.start,Math.max(begin.end,curr.end));

//  ls.add(merged);

// curr=merged;

begin=merged;

}else{

ls.add(begin);

begin=curr;

}

}

ls.add(begin);

return ls;

}

class IntervalComparator implements
Comparator<Interval>{

public int compare(Interval i1,Interval i2){

// return i1.start.compareTo(i2.start);

return
i1.start - i2.start;

}

}

}

在参考过多次leetcode后,发现这个网站http://www.programcreek.com 是个pretty good website,很多博文也是参考这上面的代码。这个地方用到了关于Collections.sort(list, new XXXComparator())

关于collections.sort的用法,下面这篇文讲的挺好:

http://blog.csdn.net/tjcyjd/article/details/6804690

也涉及思考关于comparator 和comparable的区别,对于这俩接口的区别,下面这篇文章讲的挺易于理解

http://www.cnblogs.com/sunflower627/p/3158042.html

发表在 leetcode
| 标签有 javaleetcode
发表回复

时间: 2024-08-29 12:14:23

56. Merge Intervals-leetcode-java的相关文章

56. Merge Intervals - LeetCode

Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个范围内,如果在且结束坐标大于上一个坐标就合并 Java实现: public List<Interval> merge(List<Interval> intervals) { if (intervals == null || intervals.size() == 0) return i

LeetCode --- 56. Merge Intervals

题目链接: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]. 这道题的要求是将给定的一组间隔中有重叠的进行合并. 将间隔合并,首先要找到相邻的间隔,然后看其是否有重叠,如果有,就进行合并. 因此,首先考虑对数组排序.排序的时候,只需要按

LeetCode开心刷题四十二天——56. Merge Intervals

56. Merge Intervals Medium 2509194FavoriteShare Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, mer

Merge Interval leetcode java

题目: 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]. 题解: 这道题主要难点是改写Comparator. Comparator接口定义了两个方法:compare( )和equals( ).这里给出的compare( )方法按顺序比较了两个元素: int comp

No.56 Merge Intervals

No.56 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]. 法一:直接复用之前写的insert函数,依次将区间段插入到结果集中 1 #include "stdafx.h" 2 #include <vector> 3

[Leetcode][Python]56: Merge Intervals

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 56: Merge Intervalshttps://oj.leetcode.com/problems/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]

leetCode 56.Merge Intervals (合并区间) 解题思路和方法

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. * publi

56. Merge Intervals 57. Insert Interval *HARD*

1. Merge 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)

[LeetCode] 56. Merge Intervals Java

题目: 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]. 题意及分析:给出很多个区间,合并有重叠的区间,输出最后的结果.我们分析可以得知对于两个区间interval1和interval2,如果interval1.start<=interval2.start,有一下几种情

LeetCode 56. 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]. 题目标签:Array 这道题目给了我们一个区间的list,让我们返回一个list,是合并了所有有重叠的区间之后的list.这道题目的关键在于如何判断两个区间有重叠,根据原题给的例子可以看出,在按照每个区间的start排序