-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain74.java
28 lines (26 loc) · 867 Bytes
/
Main74.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
package JZOfferTuJi;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Main74 {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
int n = intervals.length;
List<int[]> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
int L = intervals[i][0], R = intervals[i][1];
if(res.size() == 0 || res.get(res.size() - 1)[1] < L) {
res.add(new int[]{L, R});
} else {
res.get(res.size() - 1)[1] = Math.max(res.get(res.size() - 1)[1], R);
}
}
return res.toArray(new int[res.size()][]);
}
}