给出若干闭合区间,合并所有重叠的部分。
样例
给出的区间列表 => 合并后的区间列表:
[ [
[1, 3], [1, 6],
[2, 6], => [8, 10],
[8, 10], [15, 18]
[15, 18] ]
]
/**
* Definition of Interval:
* public class Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/
class Solution {
/**
* @param intervals, a collection of intervals
* @return: A new sorted interval list.
*/
public List<Interval> merge(List<Interval> intervals) {
// write your code here
if(intervals==null||intervals.size()==0){
return intervals;
}
if(intervals.size()==1){
return intervals;
}
for(int i=0;i<intervals.size();i++){
Interval temp1=intervals.get(i);
if(temp1==null){
continue;
}
for(int j=i+1;j<intervals.size();j++){
Interval temp2=intervals.get(j);
temp1=intervals.get(i);
if(temp2==null){
continue;
}
if(temp1==null){
continue;
}
if(temp2.end<temp1.start){
continue;
}else if(temp2.end>=temp1.start&&temp2.end<=temp1.end&&temp2.start<=temp1.start){
Interval replace=new Interval(temp2.start, temp1.end);
intervals.remove(j);
intervals.add(j, replace);
intervals.remove(i);
intervals.add(i, null);
}else if(temp2.start<=temp1.start&&temp2.end>=temp1.end){
Interval replace=new Interval(temp2.start, temp2.end);
intervals.remove(j);
intervals.add(j, replace);
intervals.remove(i);
intervals.add(i, null);
}else if(temp2.start>=temp1.start&&temp2.end<=temp1.end){
Interval replace=new Interval(temp1.start, temp1.end);
intervals.remove(j);
intervals.add(j, replace);
intervals.remove(i);
intervals.add(i, null);
}else if(temp2.start>=temp1.start&&temp2.end>=temp1.end&&temp2.start<=temp1.end){
Interval replace=new Interval(temp1.start, temp2.end);
intervals.remove(j);
intervals.add(j, replace);
intervals.remove(i);
intervals.add(i, null);
}else if(temp2.start>=temp1.end){
continue;
}
}
}
List<Interval> result=new ArrayList<Interval>();
for(int i=0;i<intervals.size();i++){
if(intervals.get(i)==null){
}else{
result.add(intervals.get(i));
}
}
Collections.sort(result, new Comparator<Interval>() {
@Override
public int compare(Interval o1, Interval o2) {
// TODO Auto-generated method stub
return o1.start-o2.start;
}
});
return result;
}
}