题目链接
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/489da516842849e9af9cb279405dd2d0.png)
贪心 + 排序
class Solution {
public int[][] merge(int[][] intervals) {
ArrayList<int[]> res = new ArrayList<>();
Arrays.sort(intervals, new Comparator<int[]>() {
public int compare(int[] intervals1, int[] intervals2) {
long diff = (long)intervals1[0] - (long)intervals2[0];
if (diff == 0) return 0;
return diff > 0 ? 1 : -1;
}
});
for(int i = 1; i < intervals.length; i++){
if(intervals[i][0] <= intervals[i - 1][1]){
intervals[i][0] = intervals[i - 1][0];
intervals[i][1] = Math.max(intervals[i - 1][1], intervals[i][1]);
}else{
res.add(intervals[i - 1]);
}
}
res.add(intervals[intervals.length - 1]);
return res.toArray(new int[res.size()][]);
}
}