-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMergeIntervals.java
64 lines (62 loc) · 1.81 KB
/
MergeIntervals.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
package io.ziheng.array.leetcode;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
/**
* LeetCode 56. Merge Intervals
* https://leetcode.com/problems/merge-intervals/
*/
public class MergeIntervals {
/**
* 主函数 -> 测试用例
*
* @param args
* @return void
*/
public static void main(String[] args) {
int[][] intervals = {
{2, 6, },
{1, 3, },
{15,18, },
{8, 10, },
};
MergeIntervals obj = new MergeIntervals();
// System.out.println(
// Arrays.deepToString(intervals)
// );
System.out.println(
Arrays.deepToString(
obj.merge(intervals)
)
);
}
public int[][] merge(int[][] intervals) {
if (intervals == null || intervals.length == 0) {
return new int[0][0];
}
Arrays.sort(intervals,
new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
}
);
List<int[]> resultList = new LinkedList<>();
int[] currentInterval = intervals[0];
resultList.add(currentInterval);
for (int[] interval : intervals) {
// Overlapping intervals, move the end if needed
// Disjoint intervals, add the new interval to the list
if (interval[0] <= currentInterval[1]) {
currentInterval[1] = Math.max(currentInterval[1], interval[1]);
} else {
currentInterval = interval;
resultList.add(currentInterval);
}
}
return resultList.toArray(new int[resultList.size()][]);
}
}
/* EOF */