概述
题目描述
给你一个 无重叠的 ,按照区间起始端点排序的区间列表。
在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。
示例 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
排序法(时间复杂度为O(NlogN),空间复杂度为O(N))
java实现
import java.util.ArrayList;
import java.util.Arrays;
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
if(intervals.length==0)
{ return new int[][]{newInterval}; }
ArrayList<int[]> result=new ArrayList<>();
//change int[][] intervals to ArrayList
ArrayList<int[]> temp=new ArrayList<>();
for(int[] interval:intervals)
{ temp.add(interval); }
temp.add(newInterval);
temp.sort((a,b)->a[0]-b[0]);
for(int[] interval:temp)
{
//Last element in result
int[] lastresult=result.size()==0?new int[]{-1,-1}:result.get(result.size()-1);
if(result.size()==0||lastresult[1]<interval[0])
{ result.add(interval); }
else
{ lastresult[1]=Math.max(lastresult[1],interval[1]); }
}
return result.toArray(new int[result.size()][2]);
}
}
Python实现
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
result=[]
if len(intervals)==0:
result.append(newInterval)
return result
intervals.append(newInterval)
#sort interbals based on the start time
intervals.sort(key=lambda x:x[0])
for interval in intervals:
if len(result)==0 or result[-1][1]<interval[0]:
result.append(interval)
else:
result[-1][1]=max(result[-1][1],interval[1])
return result
贪心算法(时间复杂度为O(N),空间复杂度为O(N))
java实现
import java.util.LinkedList;
import java.lang.Math;
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
LinkedList<int[]> result=new LinkedList<>();
int index=0;
//put all intervals into result when the start time smaller than newInterval's start time
for(int[] interval:intervals)
{
if(interval[0]<newInterval[0])
{
result.add(interval);
index++;
}
else { break; }
}
//put or merge newInterval into result
if(result.size()==0||result.getLast()[1]<newInterval[0])
{ result.add(newInterval); }
else{
result.getLast()[1]=Math.max(result.getLast()[1],newInterval[1]);
}
//put all rest intervals into result
for(int i=index;i<intervals.length;i++)
{
if(result.getLast()[1]<intervals[i][0])
{ result.add(intervals[i]); }
else
{ result.getLast()[1]=Math.max(result.getLast()[1],intervals[i][1]); }
}
return result.toArray(new int[result.size()][2]);
}
}
Python实现
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
result = []
index=0
#put all intervals into result when the start time smaller than newInterval’s start time
for interval in intervals:
if interval[0]<newInterval[0]:
result.append(interval)
index+=1
else:
break
#put newInterval into result if newInterval’s start time larger than lastest end time in result
if len(result)==0 or result[-1][1]<newInterval[0]:
result.append(newInterval)
else:
#merge newInterval with lastest result
result[-1][1]=max(result[-1][1],newInterval[1])
#put all rest intervals into result
for i in range(index,len(intervals)):
if result[-1][1]<intervals[i][0]:
result.append(intervals[i])
else:
result[-1][1]=max(result[-1][1],intervals[i][1])
return result
最后
以上就是眯眯眼哑铃为你收集整理的插入区间(57)力扣的全部内容,希望文章能够帮你解决插入区间(57)力扣所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复