-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ56.java
More file actions
33 lines (33 loc) · 1.02 KB
/
Q56.java
File metadata and controls
33 lines (33 loc) · 1.02 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
/*
* @Author: Shawn Yang
* @Date: 2019-09-17 14:13:29
* @Last Modified by: Shawn Yang
* @Last Modified time: 2019-09-17 14:13:39
*/
class Solution {
public int[][] merge(int[][] intervals) {
if(intervals == null || intervals.length == 0) {
return new int[0][];
}
Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
ArrayList<int[]> res = new ArrayList<>();
int start = intervals[0][0];
int end = intervals[0][1];
int length = intervals.length;
for(int i = 1; i < length; i++) {
if(intervals[i][0] <= end) {
end = Math.max(intervals[i][1], end);
} else {
res.add(new int[]{start, end});
start = intervals[i][0];
end = intervals[i][1];
}
}
res.add(new int[]{start, end});
int[][] result = new int[res.size()][];
for(int i = 0; i < res.size(); i++) {
result[i] = res.get(i);
}
return result;
}
}