simulation/merge-intervals
优质
小牛编辑
127浏览
2023-12-01
Merge Intervals
描述
Given a collection of intervals, merge all overlapping intervals.
For example, Given [1,3],[2,6],[8,10],[15,18]
, return [1,6],[8,10],[15,18]
分析
复用一下 Insert Intervals 的解法即可,创建一个新的 interval 集合,然后每次从旧的里面取一个 interval 出来,然后插入到新的集合中。
代码
// Merge Interval
// 复用一下Insert Intervals的解法即可
// 时间复杂度O(n1+n2+...),空间复杂度O(1)
public class Solution {
public int[][] merge(int[][] intervals) {
ArrayList<int[]> list = new ArrayList<>();
for (int[] newInterval : intervals) {
insert(list, newInterval);
}
return list.toArray(new int[0][2]);
}
private void insert(ArrayList<int[]> intervals, int[] newInterval) {
for (int i = 0; i < intervals.size();) {
final int[] cur = intervals.get(i);
if (newInterval[1] < cur[0]) {
intervals.add(i, newInterval);
return;
} else if (newInterval[0] > cur[1]) {
++i;
} else {
newInterval[0] = Math.min(newInterval[0], cur[0]);
newInterval[1] = Math.max(newInterval[1], cur[1]);
intervals.remove(i);
}
}
intervals.add(newInterval);
}
}
// Merge Interval
//复用一下Insert Intervals的解法即可
// 时间复杂度O(n1+n2+...),空间复杂度O(1)
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> result;
for (int i = 0; i < intervals.size(); i++) {
insert(result, intervals[i]);
}
return result;
}
private:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval>::iterator it = intervals.begin();
while (it != intervals.end()) {
if (newInterval.end < it->start) {
intervals.insert(it, newInterval);
return intervals;
} else if (newInterval.start > it->end) {
it++;
continue;
} else {
newInterval.start = min(newInterval.start, it->start);
newInterval.end = max(newInterval.end, it->end);
it = intervals.erase(it);
}
}
intervals.insert(intervals.end(), newInterval);
return intervals;
}
};