class ArrayComparator implements Comparator<Object>
{
public int compare(Object arg0, Object arg1) {
int [] a=(int[]) arg0;
int [] b=(int[]) arg1;
if(a[0]>b[0]) return 1;
else if(a[0]<b[0]) return -1;
else return 0;
}
}
public class Solution {
public static int[][] merge(int[][] intervals) {
if(intervals.length==0) return intervals;
Arrays.sort(intervals,0,intervals.length,new ArrayComparator());
int curIndex=0;
for(int i=1;i<intervals.length;i++)
{
if(intervals[curIndex][1]>=intervals[i][1])
continue;
else if(intervals[curIndex][1]>=intervals[i][0])
{
intervals[curIndex][1]=intervals[i][1];
}
else
{
curIndex++;
intervals[curIndex][0]=intervals[i][0];
intervals[curIndex][1]=intervals[i][1];
}
}
curIndex++;
int [][] ret=new int [curIndex][2];
System.arraycopy(intervals, 0, ret, 0, curIndex);
return ret;
}
}