1 public class Solution { 2 public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList<Interval> result = new ArrayList<Interval>(); 6 for (int i = 0; i < intervals.size(); i++) { 7 Interval tmp = intervals.get(i); 8 if (newInterval.end < tmp.start) { 9 result.add(newInterval); 10 for(int j = i; j < intervals.size(); j++){ 11 result.add(intervals.get(j)); 12 } 13 return result; 14 } else if (newInterval.start > tmp.end) { 15 result.add(tmp); 16 continue; 17 } else { 18 newInterval.start = Math.min(tmp.start, newInterval.start); 19 newInterval.end = Math.max(tmp.end, newInterval.end); 20 } 21 } 22 result.add(newInterval); 23 return result; 24 } 25 }