/**
* 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 List<Interval> insert(List<Interval> intervals, Interval newInterval) {
if (intervals == null) {
throw new IllegalArgumentException("");
}
if (intervals.size() == 0) {
intervals.add(newInterval);
return intervals;
}
List<Interval> res = new LinkedList<>();
boolean flag = true;
for (Interval i: intervals) {
if (i.end < newInterval.start) {
res.add(i);
} else if (newInterval.end < i.start) {
if (flag) {
res.add(newInterval);
flag = false;
}
res.add(i);
} else {
newInterval = new Interval(Math.min(i.start, newInterval.start), Math.max(i.end, newInterval.end));
}
}
if (flag) {
res.add(newInterval);
}
return res;
}
}
Insert Interval
最新推荐文章于 2020-11-04 20:53:35 发布