-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeInterval.java
73 lines (58 loc) · 1.97 KB
/
MergeInterval.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package learn.freq05;
import entity.Interval;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//Given a collection of intervals, merge all overlapping intervals.
//
//For example,
//Given [1,3],[2,6],[8,10],[15,18],
//return [1,6],[8,10],[15,18].
//其实只有 第一和第二个数组融合了
//题目给你的interval都是start小 end大
//merge 间隔
public class MergeInterval {
public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
ArrayList<Interval> result = new ArrayList<Interval>();
if (intervals == null || intervals.size() <= 1) {
return intervals;
}
Collections.sort(intervals, new IntervalComparator());
//排序好 后
//弄一个 prev=0位 然后 和从1遍历的curr比
//如果curr.start>=prev.end 说明有 interval
//那时候只要在比较prev.end和curr.end 那个end 大就更新prev.edm
//如果 curr.start<prev.end 说明没有interval
//直接 result.add(prev); prev=curr;
//循环结束后再加一次即可
Interval prev = intervals.get(0);
for (int i = 1; i < intervals.size(); i++) {
Interval curr = intervals.get(i);
if (curr.start <= prev.end) {
if (prev.end < curr.end) {
prev.end = curr.end;
}
} else {
result.add(prev);
prev = curr;
}
}
result.add(prev);
return result;
}
static class IntervalComparator implements Comparator<Interval> {
public int compare(Interval o1, Interval o2) {
if (o1.start < o2.start) {
return -1;
}
if (o1.start > o2.start) {
return 1;
}
return 0;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
// leetcode 不认类的comparator