@@ -0,0 +1,1381 @@
+// Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).%0A%0A// You may assume that the intervals were initially sorted according to their start times.%0A%0A// Example 1:%0A// Given intervals %5B1,3%5D,%5B6,9%5D, insert and merge %5B2,5%5D in as %5B1,5%5D,%5B6,9%5D.%0A%0A// Example 2:%0A// Given %5B1,2%5D,%5B3,5%5D,%5B6,7%5D,%5B8,10%5D,%5B12,16%5D, insert and merge %5B4,9%5D in as %5B1,2%5D,%5B3,10%5D,%5B12,16%5D.%0A%0A// This is because the new interval %5B4,9%5D overlaps with %5B3,5%5D,%5B6,7%5D,%5B8,10%5D.%0A%0A/**%0A * Definition for an interval.%0A * public class Interval %7B%0A *     int start;%0A *     int end;%0A *     Interval() %7B start = 0; end = 0; %7D%0A *     Interval(int s, int e) %7B start = s; end = e; %7D%0A * %7D%0A */%0Apublic class Solution %7B%0A    public List%3CInterval%3E insert(List%3CInterval%3E intervals, Interval newInterval) %7B%0A    %09if(newInterval == null) return intervals;%0A    %09if(intervals.size() == 0)%7B%0A    %09%09intervals.add(newInterval);%0A    %09%09return intervals;%0A    %09%7D%0A%0A    %09List%3CInterval%3E list = new ArrayList%3CInterval%3E();%0A%0A    %09int pos = 0;%0A%0A    %09for(Interval i : intervals)%7B%0A    %09%09if(i.end %3C newInterval.start)%7B%0A    %09%09%09list.add(i);%0A    %09%09%09pos++;%0A    %09%09%7Delse if(i.start %3E newInterval.end)%7B%0A    %09%09%09list.add(i);%0A    %09%09%7Delse%7B%0A    %09%09%09newInterval.start = Math.min(newInterval.start, i.start);%0A    %09%09%09newInterval.end = Math.max(newInterval.end, i.end);%0A    %09%09%7D%0A    %09%7D%0A%0A    %09list.add(pos, newInterval);%0A%0A    %09return list;%0A%09%7D%0A%7D%0A

add insert interval solution