码迷,mamicode.com
首页 > 其他好文 > 详细

【Insert Interval】cpp

时间:2015-06-06 19:36:03      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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

代码:

/**
 * 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)
        {
            vector<Interval> ret;
            int i = 0;
            // search for start insert position
            for ( ; i<intervals.size(); ++i )
            {
                if ( newInterval.start > intervals[i].end )
                {
                    ret.push_back(intervals[i]);
                }
                else
                {
                    break;
                }
            }
            // newInterval larger than all the existed intervals
            if ( i==intervals.size() )
            {
                ret.push_back(newInterval);
                return ret;
            }
            int start = std::min( intervals[i].start, newInterval.start );
            // search for the end insert position
            for ( ;i<intervals.size();++i )
            {
                if ( newInterval.end <= intervals[i].end ) break;
            }
            // newInterval end is larger than all the range
            if ( i==intervals.size() )
            {
                ret.push_back(Interval(start, newInterval.end));
                return ret;
            }
            if ( newInterval.end<intervals[i].start )
            {
                ret.push_back(Interval(start,newInterval.end));
                ret.insert(ret.end(), intervals.begin()+i, intervals.end());
                return ret;
            }
            if ( newInterval.end==intervals[i].start )
            {
                ret.push_back(Interval(start,intervals[i].end));
                if ( i<intervals.size()-1 )
                {
                    ret.insert(ret.end(), intervals.begin()+i+1, intervals.end());
                }
                return ret;
            }
            if ( newInterval.end > intervals[i].start )
            {
                ret.push_back(Interval(start,intervals[i].end));
                if ( i<intervals.size()-1 )
                {
                    ret.insert(ret.end(), intervals.begin()+i+1,intervals.end());
                }
                return ret;
            }
        }
};

tips:

这道题的总体感觉就是很繁琐,因为要考虑各种边界情况,等或者不等;虽然能AC但是这一版代码比较丑陋。

看能不能改一版漂亮一些的。

【Insert Interval】cpp

标签:

原文地址:http://www.cnblogs.com/xbf9xbf/p/4557060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!