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]
.
Java:
1. http://blog.csdn.net/linhuanmars/article/details/21857617
2. 备用:http://blog.csdn.net/fightforyourdream/article/details/16882295
熟悉下自建 Comparator 还比较清晰易懂
/**
* 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 ArrayList<Interval> merge(ArrayList<Interval> intervals) {
if(intervals==null||intervals.size()==0) return intervals;
ArrayList<Interval> res= new ArrayList<Interval>();
Comparator<Interval> comp=new Comparator<Interval>(){
@Override
public int compare(Interval i1,Interval i2)
{
if(i1.start==i2.start) return i1.end-i2.end;
return i1.start-i2.start;
}
};
Collections.sort(intervals, comp);
res.add(intervals.get(0));
for(int i=1;i<intervals.size();i++)
{
if(res.get(res.size()-1).end>=intervals.get(i).start)
{
res.get(res.size()-1).end=Math.max(res.get(res.size()-1).end, intervals.get(i).end);
}
else{
res.add(intervals.get(i));
}
}
return res;
}
}