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


在原始数组上操作,先按照start值在原数组中二分查找待插入的区间,假设查找到的位置为ite,从ite或者ite-1开始合并区间直到不能合并为止(终止条件是合并后区间的end<当前区间的start),然后在原数组中删除参与合并的区间,再插入合并后的新区

间         本文地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class  Solution {
private :
     static  bool  comp(Interval a, Interval b)
     {
         return  a.start < b.start;
     }
public :
     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
         //在原始数组上操作
         vector<Interval>::iterator ite = lower_bound(intervals.begin(),intervals.end(), newInterval, comp); //按照start值二分查找
         if (ite != intervals.begin() && newInterval.start <= (ite-1)->end) //ite的上一个区间也可能参与合并
         {
             ite--;
             //合并后新区间的起点只和第一个合并的区间有关,因为数组时按区间起点有序的
             newInterval.start = min(newInterval.start, ite->start);
         }
         vector<Interval>::iterator eraseBegin = ite;
         for (; ite != intervals.end() && newInterval.end >= ite->start; ite++)
             if (newInterval.end < ite->end)newInterval.end = ite->end; //合并后的新区间存放于newInterval
         
         ite = intervals.erase(eraseBegin, ite); //[eraseBegin, ite)是合并时应该删掉的区间
         intervals.insert(ite, newInterval); //插入合并后的区间
         return  intervals;
     }
};

 

新建数组存放结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
  * 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 {
private :
     static  bool  comp(Interval a, Interval b)
     {
         return  a.start < b.start;
     }
public :
     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
         vector<Interval> res;
         res.reserve(intervals.size());
         int  i;
         //插入前部分不需要合并的区间
         for (i = 0; i < intervals.size() && intervals[i].end < newInterval.start; i++)
             res.push_back(intervals[i]);
         //i为需要合并的起点,注意的是合并后新区间的起点只和第一个合并的区间有关,因为数组时按区间起点有序的
         if (i < intervals.size())newInterval.start = min(newInterval.start, intervals[i].start);
         
         //合并区间
         for (; i < intervals.size() && newInterval.end >= intervals[i].start; i++)
             if (newInterval.end < intervals[i].end)newInterval.end = intervals[i].end;
         //插入合并后的区间
         res.push_back(newInterval);
         //插入剩余的区间
         res.insert(res.end(), intervals.begin()+i, intervals.end());
         return  res;
     }
};

 






本文转自tenos博客园博客,原文链接:http://www.cnblogs.com/TenosDoIt/p/3715013.html,如需转载请自行联系原作者

目录
相关文章
|
JavaScript 索引
LeetCode 436. Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
78 0
LeetCode 436. Find Right Interval
|
存储 索引
LeetCode 381. Insert Delete GetRandom O1 Dallowed
设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构。
75 0
LeetCode 381. Insert Delete GetRandom O1 Dallowed
|
存储 索引
LeetCode 380. Insert Delete GetRandom O1
设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构。
43 0
LeetCode 380. Insert Delete GetRandom O1
LeetCode之Search Insert Position
LeetCode之Search Insert Position
95 0
|
存储 算法 Java
LeetCode 380: 常数时间插入、删除和获取随机元素 Insert Delete GetRandom O(1)
题目: 设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构。 insert(val):当元素 val 不存在时,向集合中插入该项。 remove(val):元素 val 存在时,从集合中移除该项。
1068 0