-
Notifications
You must be signed in to change notification settings - Fork 31
/
56.java
27 lines (24 loc) · 973 Bytes
/
56.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
class Solution {
public int[][] merge(int[][] intervals) {
if (intervals.length <= 1){
return intervals;
}
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
List<int[]> result = new ArrayList<>();
int[] current_interval = intervals[0];
result.add(current_interval);
for(int[] interval : intervals){
int current_first = current_interval[0];
int current_last = current_interval[1];
int next_first = interval[0];
int next_last = interval[1];
if(current_last >= next_first){
current_interval[1] = Math.max(current_last,next_last);
}else{
current_interval = interval;
result.add(current_interval);
}
}
return result.toArray(new int[result.size()][2]);
}
}