-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution056.java
46 lines (38 loc) · 1000 Bytes
/
Solution056.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
package algorithm.leetcode;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* @author: mayuan
* @desc: 合并区间
* @date: 2019/01/13
*/
public class Solution056 {
public List<Interval> merge(List<Interval> intervals) {
if (null == intervals) {
return intervals;
}
Collections.sort(intervals, (a, b) -> a.start - b.start);
LinkedList<Interval> result = new LinkedList<>();
for (Interval e : intervals) {
if (result.isEmpty() || result.getLast().end < e.start) {
result.add(e);
} else {
result.getLast().end = Math.max(result.getLast().end, e.end);
}
}
return result;
}
private class Interval {
int start;
int end;
Interval() {
start = 0;
end = 0;
}
Interval(int s, int e) {
start = s;
end = e;
}
}
}