Leetcode57 Insert Interval

Posted xuweimdm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode57 Insert Interval相关的知识,希望对你有一定的参考价值。

Insert Interval

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].

Solution1

  • 这道题的思路是这样:由于原始的intervals是有序的,所以找到待插入的interval的插入位置,然后往后找interval的右端点能够伸展到哪个地方,并将伸展过程中碰到的原始interval进行合并。思路其实还是很简单的,但是写出来却很容易出错。
public class Solution 
    public List<Interval> insert(List<Interval> intervals, Interval newInterval) 
        List<Interval> result = new ArrayList<Interval>();
        int index = binarySearch(intervals, newInterval.start, 0, intervals.size()-1);
        for(int j=0;j<index;j++) result.add(intervals.get(j));
        if(index>-1&&intervals.get(index).end>=newInterval.start)
            newInterval.start = intervals.get(index).start;
        else if(index>-1) result.add(intervals.get(index));
        int i = index+1;
        for(;i<intervals.size();i++) if(intervals.get(i).start>newInterval.end) break;
        newInterval.end = Math.max(newInterval.end, i>0?intervals.get(i-1).end:newInterval.end);
        result.add(newInterval);
        for(;i<intervals.size();i++) result.add(intervals.get(i));
        return result;
    
    public int binarySearch(List<Interval> intervals, int point, int start, int end)//二分法找到待插入的位置
        if(start>end) return end;
        int mid = (start+end)/2;
        if(intervals.get(mid).start<=point&&(mid+1>end||intervals.get(mid+1).start>point)) return mid;
        if(intervals.get(mid).start>point) return binarySearch(intervals,point,start,mid-1);
        else return binarySearch(intervals,point,mid+1,end);        
    

Solution2

  • 也可以直接在原intervals上直接操作。
public class Solution 
    public List<Interval> insert(List<Interval> intervals, Interval newInterval) 
        int index = binarySearch(intervals, newInterval.start, 0, intervals.size()-1);
        int i = index+1;
        for(;i<intervals.size();i++)
            if(intervals.get(i).start>newInterval.end) break;
        
        newInterval.end = Math.max(newInterval.end, i>0?intervals.get(i-1).end:newInterval.end);
        while(--i>index) intervals.remove(index+1);
        intervals.add(index+1,newInterval);
        if(index>-1&&intervals.get(index).end>=newInterval.start)
            newInterval.start = intervals.get(index).start;
            intervals.remove(index);
        
        return intervals;
    
    public int binarySearch(List<Interval> intervals, int point, int start, int end)
        if(start>end) return end;
        int mid = (start+end)/2;
        if(intervals.get(mid).start<=point&&(mid+1>end||intervals.get(mid+1).start>point)) return mid;
        if(intervals.get(mid).start>point) return binarySearch(intervals,point,start,mid-1);
        else return binarySearch(intervals,point,mid+1,end);        
    

Solution3

  • 实际上,解法一和解法二的二分查找并不会使得时间复杂度降低。不使用二分查找,这道题如此写将会更加清晰易懂。
public class Solution 
    public List<Interval> insert(List<Interval> intervals, Interval newInterval) 
        List<Interval> result = new ArrayList<Interval>();
        int i=0;
        for(;i<intervals.size()&&intervals.get(i).end<newInterval.start;i++) result.add(intervals.get(i));
        if(i==intervals.size())
            result.add(newInterval);
            return result;
        
        int j=i;
        for(;j<intervals.size()&&intervals.get(j).start<=newInterval.end;j++);
        newInterval.start = Math.min(newInterval.start,intervals.get(i).start);
        newInterval.end = Math.max(newInterval.end,j>0?intervals.get(j-1).end:newInterval.end);
        result.add(newInterval);
        for(;j<intervals.size();j++) result.add(intervals.get(j));
        return result;
    

以上是关于Leetcode57 Insert Interval的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode57 Insert Interval

LeetCode57 Insert Interval

Leetcode 57: Insert Interval

leetcode 57. Insert Interval

[LeetCode] 57. Insert Interval Java

leetcode57- Insert Interval- medium