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. * 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 Listmerge(List intervals) { List res = new ArrayList<>(); for(int i=0;i insert(List intervals, Interval newInterval) { List list = new ArrayList (); if(intervals.size()==0){ list.add(newInterval); return list; } int start = newInterval.start; int end = newInterval.end; int mstart=-1; int mend=-1; int nstart = -1; int nend = -1; for(int i=0;i intervals.get(intervals.size()-1).end){ list = intervals; list.add(newInterval); }else if(end