区间合并:
代码实现
public int[][] merge(int[][] intervals) {
// 特殊情况处理
if (intervals == null || intervals.length <= 1) {
return intervals;
}
Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]);
List<int[]> list = new ArrayList<>();
int[] pre = intervals[0];
for (int i = 0; i < intervals.length; i++) {
if (pre[1] >= intervals[i][0]) {
pre[1] = Math.max(pre[1], intervals[i][1]);
} else {
list.add(pre);
pre = intervals[i];
}
}
list.add(pre);
return list.toArray(new int[list.size()][2]);
}