Question:
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9]
, insert and merge [2,5]
in as [1,5],[6,9]
.
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16]
, insert and merge [4,9]
in as [1,2],[3,10],[12,16]
.
This is because the new interval [4,9]
overlaps with [3,5],[6,7],[8,10]
.
solution:
看了思路后写了一个LTE的。。
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
intervals.add(newInterval);
int n=intervals.size();
int i=0;
int j=0;
//sort with start
for(i=0;i<=n-2;i++)
{
for(j=i+1;j<=n-1;j++)
{
if(intervals.get(i).start>intervals.get(j).start)
{
Interval tmp=intervals.get(i);
intervals.set(i,intervals.get(j));
intervals.set(j,tmp);
}
}
}
//take the first interval and compare its end with the next interval starts
for(i=0;i<=n-2;i++)
{
if(intervals.get(i).end>intervals.get(i+1).start&&(intervals.get(i).end<=intervals.get(i+1).end))
{
Interval tmp=new Interval(intervals.get(i).start,intervals.get(i+1).start);
intervals.set(i,tmp);
}
if(intervals.get(i).end>intervals.get(i+1).start&&(intervals.get(i).end<=intervals.get(i+1).end))
{
Interval tmp=new Interval(intervals.get(i).start,intervals.get(i+1).start);
intervals.set(i,tmp);
}
}
//combine two interval if its end is the same as the next interval's start
for(i=n-1;i>=1;i--)
{
if(intervals.get(i).start==intervals.get(i-1).end)
{
Interval tmp=new Interval(intervals.get(i-1).start,intervals.get(i).end);
intervals.remove(i);
intervals.set(i-1,tmp);
}
}
return intervals;
}
}
修改了下插入那里,就过了~但还有更好的方法
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
intervals.add(newInterval);
int n=intervals.size();
int i=0;
int j=0;
//sort with start,to find where to put the newinterval
for(i=n-2;i>=0;i--)
{
if(intervals.get(n-1).start>=intervals.get(i).start)
break;
}
int piv=i;
//add the new interval
for(i=n-1;i>=piv+2;i--)
{
intervals.set(i,intervals.get(i-1));
}
intervals.set(piv+1,newInterval);
//take the first interval and compare its end with the next interval starts
for(i=0;i<=n-2;i++)
{
if(intervals.get(i).end>intervals.get(i+1).start&&(intervals.get(i).end<=intervals.get(i+1).end))
{
// Interval tmp=new Interval(intervals.get(i).start,intervals.get(i+1).start);
intervals.set(i,new Interval(intervals.get(i).start,intervals.get(i+1).start));
}
if(intervals.get(i).end>intervals.get(i+1).start&&(intervals.get(i).end>intervals.get(i+1).end))
{
// Interval tmp=new Interval(intervals.get(i).end,intervals.get(i).end);
intervals.set(i+1,new Interval(intervals.get(i).end,intervals.get(i).end));
}
}
//combine two interval if its end is the same as the next interval's start
for(i=n-1;i>=1;i--)
{
if(intervals.get(i).start==intervals.get(i-1).end)
{
// Interval tmp=new Interval(intervals.get(i-1).start,intervals.get(i).end);
intervals.set(i-1,new Interval(intervals.get(i-1).start,intervals.get(i).end));
intervals.remove(i);
}
}
return intervals;
}
}
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> ans=new LinkedList<>();
int n=intervals.size();
int i=0;
//put the unrevelvant item into the ans
while(i<=n-1&&intervals.get(i).end<newInterval.start)
ans.add(intervals.get(i++));
//change the newInterval
while(i<=n-1&&intervals.get(i).start<=newInterval.end)
{
newInterval=new Interval(
Math.min(intervals.get(i).start,newInterval.start),
Math.max(intervals.get(i).end, newInterval.end));
i++;
}
ans.add(newInterval);
while(i<=n-1)
ans.add(intervals.get(i++));
return ans;
}
}