@@ -0,0 +1,1519 @@
+package leetcode.merge_intervals;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0A%0Aimport common.Interval;%0A%0Apublic class Solution %7B%0A    public ArrayList%3CInterval%3E merge(ArrayList%3CInterval%3E intervals) %7B%0A    %09ArrayList%3CInterval%3E ans = new ArrayList%3CInterval%3E();%0A    %09for(int i = intervals.size() - 1; i %3E= 0; i--)%7B%0A    %09%09Interval iv = intervals.get(i);%0A    %09%09if(iv.start %3E iv.end)%7B%0A    %09%09%09int tmp = iv.start;%0A    %09%09%09iv.start = iv.end;%0A    %09%09%09iv.end = tmp;%0A    %09%09%7D%0A    %09%7D%0A    %09int size = intervals.size();%0A    %09if(intervals.size() == 0) return ans;%0A    %09Collections.sort(intervals, new Comparator%3CInterval%3E()%7B%0A%09%09%09@Override%0A%09%09%09public int compare(Interval iv1, Interval iv2) %7B%0A%09%09%09%09if(iv1.start %3C iv2.start) return -1;%0A%09%09%09%09if(iv1.start %3E iv2.start) return 1;%0A%09%09%09%09return 0;%0A%09%09%09%7D%0A    %09%7D);%0A    %09Interval cur = intervals.get(0);%0A    %09int index = 1;%0A    %09while(true)%7B%0A    %09%09while(index %3C size && intervals.get(index).start %3C= cur.end)%7B%0A    %09%09%09cur.end = Math.max(cur.end, intervals.get(index).end);%0A    %09%09%09index++;%0A    %09%09%7D%0A    %09%09ans.add(cur);%0A    %09%09if(index %3C size)%0A    %09%09%09cur = intervals.get(index); %0A    %09%09else%0A    %09%09%09break;%0A    %09%7D%0A    %09return ans;%0A    %7D%0A    %0A    public static void main(String%5B%5D args)%7B%0A    %09ArrayList%3CInterval%3E input = new ArrayList%3CInterval%3E();%0A    %09//%5B1,3%5D,%5B2,6%5D,%5B8,10%5D,%5B15,18%5D%0A    %09input.add(new Interval(0, 4));%0A    %09input.add(new Interval(0, 0));%0A    %09for(Interval iv : new Solution().merge(input))%7B%0A    %09%09System.err.println(iv.start + %22,%22 + iv.end);%0A    %09%7D%0A    %7D%0A%7D%0A

Add java solution for 56. Merge Intervals