LeetCode57 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]. (Hard)

分析:

首先可以采用merge interval的方法,先把区间填进去,然后排序,最后再调用merge,复杂度O(NlogN)

代码:

 1 /**
 2  * Definition for an interval.
 3  * struct Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() : start(0), end(0) {}
 7  *     Interval(int s, int e) : start(s), end(e) {}
 8  * };
 9  */
10 class Solution {
11 private:
12     static bool cmp (const Interval& I1, const Interval& I2) {
13         return I1.start < I2.start;
14     }
15     vector<Interval> merge(vector<Interval>& intervals) {
16         vector<Interval> result;
17         if (intervals.size() == 0) {
18             return result;
19         }
20         sort(intervals.begin(), intervals.end(), cmp);
21         int left = intervals[0].start, right = intervals[0].end;
22         for (int i = 1; i < intervals.size(); ++i) {
23             if (intervals[i].start <= right) {
24                 right = max(right,intervals[i].end);
25             }
26             else {
27                 result.push_back(Interval(left,right));
28                 left = intervals[i].start;
29                 right = intervals[i].end;
30             }
31         }
32         result.push_back(Interval(left,right));
33         return result;
34     }
35 public:
36     vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
37         vector<Interval> result;
38         intervals.push_back(newInterval);
39         sort(intervals.begin(),intervals.end(),cmp);
40         result = merge(intervals);
41         return result;
42     }
43 };

当然还有O(N)的算法可以优化。

分三步来做:

第一步,找到左侧不跟newInterval相交的区间添加到结果中;

第二步,找到所有和newInterval相交的区间并找到其左边界和右边界,然后建立新的interval添加到结果中;

第三部,找到右侧不跟newInterval相交的区间添加到结果中。

注意很多细节在里面可能会犯错

代码:

 1 /**
 2  * Definition for an interval.
 3  * struct Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() : start(0), end(0) {}
 7  *     Interval(int s, int e) : start(s), end(e) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
13         vector<Interval> result;
14         if (intervals.size() == 0) {
15             result.push_back(newInterval);
16             return result;
17         }
18         int i = 0;
19         while (i < intervals.size() && intervals[i].end < newInterval.start) {
20             result.push_back(intervals[i++]);
21         }
22         int left = 0;
23         if (i == intervals.size()) {  
24             left = newInterval.start;                   
25         }
26         else {
27             left = min(newInterval.start,intervals[i].start);
28         }
29         while (i < intervals.size() && intervals[i].start <= newInterval.end) {
30             i++;
31         }
32         int right = 0;
33         if (i >= 1) {
34             right = max(newInterval.end, intervals[i - 1].end);
35         }
36         else {
37             right = newInterval.end;
38         }
39         result.push_back(Interval(left,right));
40         while (i < intervals.size() ) {
41             result.push_back(intervals[i++]);
42         }
43         return result;
44     }
45 };

 

 
posted @ 2016-08-30 22:48  wangxiaobao1114  阅读(176)  评论(0编辑  收藏  举报