public class Solution { public Listinsert(List intervals, Interval newInterval) { List res = new ArrayList (); if (intervals == null || intervals.isEmpty()) { res.add(newInterval); return res; } if (newInterval == null) { return intervals; } int insertPos = 0; for (Interval interval : intervals) { if (interval.end < newInterval.start) { res.add(interval); insertPos++; } else if (interval.start > newInterval.end) { res.add(interval); } else { newInterval.start = Math.min(interval.start, newInterval.start); newInterval.end = Math.max(interval.end, newInterval.end); } } res.add(insertPos, newInterval); return res; }}
参考九章答案