-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q_MergingInterval.java
59 lines (56 loc) · 1.83 KB
/
Q_MergingInterval.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
import java.util.ArrayList;
import java.util.Comparator;
/**
* @author wmy
* @date 2021/6/13 21:45
*/
/*
描述
给出一组区间,请合并所有重叠的区间。
请保证合并后的区间按区间起点升序排列。
示例1
输入:
[[10,30],[20,60],[80,100],[150,180]]
复制
返回值:
[[10,60],[80,100],[150,180]]
复制
*/
public class Q_MergingInterval {
public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
ArrayList<Interval> res = new ArrayList<>();
int size = intervals.size();
if (size == 0) {
return res;
}
intervals.sort(new Comparator<Interval>() {
@Override
public int compare(Interval a, Interval b) {
return a.start - b.start;
}
});
for (int i = 1; i < size; i++) {
if (intervals.get(i).start <= intervals.get(i - 1).end) {
intervals.get(i).start = intervals.get(i - 1).start;
intervals.get(i).end = Math.max(intervals.get(i - 1).end, intervals.get(i).end);
} else {
res.add(intervals.get(i - 1));
}
}
res.add(intervals.get(size - 1));
return res;
}
public static void main(String[] args) {
Q_MergingInterval app = new Q_MergingInterval();
int[][] matrix = {{10, 30}, {20, 60}, {80, 100}, {150, 180}};
ArrayList<Interval> intervals = new ArrayList<>();
intervals.add(new Interval(10, 30));
intervals.add(new Interval(20, 60));
intervals.add(new Interval(80, 100));
intervals.add(new Interval(150, 180));
ArrayList<Interval> res = app.merge(intervals);
for (Interval re : res) {
System.out.println(re.start + " " + re.end);
}
}
}