给你一个 无重叠的 ,按照区间起始端点排序的区间列表。
在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。
示例 1:
输入:intervals = [[1,3],[6,9]], newInterval = [2,5] 输出:[[1,5],[6,9]]
示例 2:
输入:intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] 输出:[[1,2],[3,10],[12,16]] 解释:这是因为新的区间[4,8]
与[3,5],[6,7],[8,10]
重叠。
示例 3:
输入:intervals = [], newInterval = [5,7] 输出:[[5,7]]
示例 4:
输入:intervals = [[1,5]], newInterval = [2,3] 输出:[[1,5]]
示例 5:
输入:intervals = [[1,5]], newInterval = [2,7] 输出:[[1,7]]
提示:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= intervals[i][0] <= intervals[i][1] <= 105
intervals
根据intervals[i][0]
按 升序 排列newInterval.length == 2
0 <= newInterval[0] <= newInterval[1] <= 105
方法一:区间合并
区间合并,将所有存在交集的区间进行合并。方法是:先对区间按照左端点升序排列,然后遍历区间进行合并。
class Solution:
def insert(
self, intervals: List[List[int]], newInterval: List[int]
) -> List[List[int]]:
def merge(intervals):
intervals.sort()
ans = []
st, ed = intervals[0]
for s, e in intervals[1:]:
if ed < s:
ans.append([st, ed])
st, ed = s, e
else:
ed = max(ed, e)
ans.append([st, ed])
return ans
intervals.append(newInterval)
return merge(intervals)
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> list = new LinkedList<>();
int i = 0;
while ((i < intervals.length) && (intervals[i][1] < newInterval[0]))
list.add(intervals[i++]);
while ((i < intervals.length) && (intervals[i][0] <= newInterval[1])) {
newInterval[0] = Math.min(intervals[i][0], newInterval[0]);
newInterval[1] = Math.max(intervals[i][1], newInterval[1]);
i++;
}
list.add(newInterval);
while (i < intervals.length) list.add(intervals[i++]);
return list.toArray(new int[list.size()][]);
}
}
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
intervals.push_back(newInterval);
return merge(intervals);
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
int st = intervals[0][0], ed = intervals[0][1];
vector<vector<int>> ans;
for (int i = 1; i < intervals.size(); ++i) {
int s = intervals[i][0], e = intervals[i][1];
if (ed < s) {
ans.push_back({st, ed});
st = s, ed = e;
} else
ed = max(ed, e);
}
ans.push_back({st, ed});
return ans;
}
};
func insert(intervals [][]int, newInterval []int) [][]int {
intervals = append(intervals, newInterval)
return merge(intervals)
}
func merge(intervals [][]int) [][]int {
sort.Slice(intervals, func(i, j int) bool {
return intervals[i][0] < intervals[j][0]
})
st, ed := intervals[0][0], intervals[0][1]
var ans [][]int
for _, e := range intervals[1:] {
if ed < e[0] {
ans = append(ans, []int{st, ed})
st, ed = e[0], e[1]
} else if ed < e[1] {
ed = e[1]
}
}
ans = append(ans, []int{st, ed})
return ans
}