-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM89.java
More file actions
60 lines (56 loc) · 1.72 KB
/
BM89.java
File metadata and controls
60 lines (56 loc) · 1.72 KB
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
54
55
56
57
58
59
60
package NiukeTOP101;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class BM89 {
public static ArrayList<Interval> merge(ArrayList<Interval> intervals) {
ArrayList<Interval> res = new ArrayList<>();
if(intervals.size() == 0){
return res;
}
if(intervals.size() == 1){
res.add(intervals.get(0));
return res;
}
Collections.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval o1, Interval o2) {
if(o1.start != o2.start){
return o1.start - o2.start;
}else{
return o1.end - o2.end;
}
}
});
// 放入第一个区间
res.add(intervals.get(0));
int count = 0;
for (int i = 1; i < intervals.size(); i++) {
Interval o1 = intervals.get(i);
Interval origin = res.get(count);
if(o1.start > origin.end){
res.add(o1);
count++;
}else{
// 区间有重叠,更新结尾
res.remove(count);
Interval s = new Interval(origin.start, o1.end);
if(o1.end < origin.end)
s.end = origin.end;
res.add(s);
}
}
return res;
}
public static void main(String[] args) {
ArrayList<Interval> intervals = new ArrayList<>();
intervals.add(new Interval(1 ,3));
merge(intervals);
}
}
class Interval {
int start;
int end;
Interval() { start = 0; end = 0; }
Interval(int s, int e) { start = s; end = e; }
}