-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution057.java
53 lines (45 loc) · 1.26 KB
/
Solution057.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package algorithm.leetcode;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* @author: mayuan
* @desc: 插入区间
* @date: 2019/01/13
*/
public class Solution057 {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
if (null == intervals || null == newInterval) {
return intervals;
}
int i = 0;
LinkedList<Interval> list = new LinkedList<>();
while (i < intervals.size() && intervals.get(i).end < newInterval.start) {
list.add(intervals.get(i));
++i;
}
while (i < intervals.size() && intervals.get(i).start <= newInterval.end) {
newInterval.start = Math.min(intervals.get(i).start, newInterval.start);
newInterval.end = Math.max(intervals.get(i).end, newInterval.end);
++i;
}
list.add(newInterval);
while (i < intervals.size()) {
list.add(intervals.get(i));
++i;
}
return list;
}
private class Interval {
int start;
int end;
Interval() {
start = 0;
end = 0;
}
Interval(int s, int e) {
start = s;
end = e;
}
}
}