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

思路

找 newInterval 的位置,然后插入。

分三种情况:比如 newInterval = [4 6]

(1)[1 2] , [7 9] 直接插入

(2)[1 2] , [5 9] 这时需要将 [4 6] 和 [5 9] 合并,即将只需修改原数组中的第 i 个间隔即可  intervals[i].start = min(newInterval.start,intervals[i].start);  

(3)[1 2] , [3 5] 这时需要将 [4 6] 和 [3 5] 合并,如果要修改原数组中的第 i 个间隔的话,需要修改start和end,可能会导致 i+1的start 与合并后的interval 交叉,所以我们要做的是合并后赋值给newInterval ,因为这样的话 newInterval 只需修改一个值即start,还需将intervals[i]删除,然后继续合并。


/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int i=0;
        for(i=0;i<intervals.size();i++)
        {
            if(newInterval.start>intervals[i].end)
                continue ;
            if(newInterval.end<intervals[i].start)
            {
                intervals.insert(intervals.begin()+i,1,newInterval);
                return intervals;
            }
            else if(newInterval.end<intervals[i].end)
                 {
                     intervals[i].start = min(newInterval.start,intervals[i].start);
                     return intervals;
                 }
                 else
                 {
                     newInterval.start = min(newInterval.start,intervals[i].start);
                     intervals.erase(intervals.begin()+i);
                     i--;
                 }
        } 
        intervals.insert(intervals.begin()+i, 1, newInterval);
        return intervals;
    }
};

 
注意 for(i=0;i<intervals.size();i++) 里面的intervals.size();是随时有可能变化的,所以不要还用int num=intervals.size();  for(i=0;i<num;i++) 类记录了。

同样这种方法也可用于上一篇博文  Merge Intervals


Quickly summarize 3 cases. Whenever there is intersection, created a new interval.

insert-interval


最新 java

/**
 * 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) {
        List<Interval> result = new ArrayList<Interval>();
        //Already sorted by start.
        // Collections.sort(intervals, new Comparator<Interval>() {
        // 	public int compare(Interval i1, Interval i2) {
        // 		return i1.start - i2.start;
        // 	}
        // });
        if(intervals == null || intervals.size()==0){
            result.add(newInterval);
            return result;
        }
        
        for(int i=0; i<intervals.size(); i++){
            Interval cur = intervals.get(i);
            // find the position to insert
            if(newInterval.start > cur.end){
                result.add(cur);
                //have no insection, just add to result
            } else if(newInterval.end < cur.start){
                result.add(newInterval);
                newInterval = cur;
                //have insection, first to merge, then go next
            } else if(newInterval.start <= cur.end || newInterval.end >= cur.start) {
                newInterval = new Interval(Math.min(newInterval.start, cur.start), Math.max(newInterval.end, cur.end));
            }
        }
        result.add(newInterval);
        return result;
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值