forked from lizeyang18/byteDanceAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest26.java
More file actions
37 lines (35 loc) · 1022 Bytes
/
test26.java
File metadata and controls
37 lines (35 loc) · 1022 Bytes
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
package byteDance;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by lizeyang on 2020/5/15.
* 合并区间
* 输入: [[1,4],[4,5]]
* 输出: [[1,5]]
*/
public class test26 {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals,(v1, v2)->v1[0]-v2[0]);
List<List<Integer>> res = new ArrayList<>();
int i = 0;
while(i<intervals.length){
List<Integer> list = new ArrayList<>();
int tmp = intervals[i][1];
list.add(intervals[i][0]);
while(i+1<intervals.length&&tmp>=intervals[i+1][0]){
tmp = intervals[i+1][1]>tmp?intervals[i+1][1]:tmp;
i++;
}
list.add(tmp);
res.add(list);
i++;
}
int[][] nums = new int[res.size()][2];
for(int j=0;j<res.size();j++){
nums[j][0] = res.get(j).get(0);
nums[j][1] = res.get(j).get(1);
}
return nums;
}
}