【Merge Intervals】cpp

题目:

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

代码:

/**
 * 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:
        static bool comp(Interval a, Interval b)
        {
            return a.start < b.start;
        }
        static vector<Interval> merge(vector<Interval>& intervals)
        {
            if (intervals.empty()) return intervals;
            vector<Interval> ret;
            std::sort(intervals.begin(), intervals.end(), Solution::comp);
            int start = intervals[0].start;
            int end = intervals[0].end;
            for ( int i=1; i<intervals.size(); ++i )
            {
                if ( intervals[i].start>end )
                {
                    ret.push_back(Interval(start,end));
                    start = intervals[i].start;
                    end = intervals[i].end;
                    continue;
                }
                if ( intervals[i].start<=end )
                {
                    start = std::min(start, intervals[i].start);
                    end = std::max(end, intervals[i].end);
                    continue;
                }
            }
            ret.push_back(Interval(start,end));
            return ret;
        }
};

tips:

一开始理解题意有误,题中没说已经按照start对intervals排序了,所以先对intervals按照start排序(构造一个comp比较器)。接下来就是常规的思路了,每次判断当前interval的start end与之前start end的大小比较。

=========================================

还有一种解法是沿用insert interval这道题的思路,每次新插入一个interval即可,代码如下.

/**
 * 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> merge(vector<Interval>& intervals)
        {
            vector<Interval> ret;
            for ( int i=0; i<intervals.size(); ++i )
            {
                ret = Solution::insert(ret, intervals[i]);
            }
            return ret;
        }
        static 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;
            }
            return ret;
        }
};

tips:这相当于是对一个区间进行插入排序,之前做的都是针对数字进行插入排序。这道题考察的点还是很好的。

===============================================

第二次过这道题,就是先按照start进行排序,再merge。

/**
 * 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:
        static bool compare(Interval a, Interval b)
        {
            return a.start < b.start;
        }
        vector<Interval> merge(vector<Interval>& intervals) 
        {
            vector<Interval> ret;
            if ( intervals.empty() ) return ret;
            sort(intervals.begin(), intervals.end(), Solution::compare);
            ret.push_back(*intervals.begin());
            for ( vector<Interval>::iterator i=intervals.begin()+1; i!=intervals.end(); ++i )
            {
                if ( i->start > ret.back().end )
                {
                    ret.push_back(*i);
                }
                else
                {
                    ret.back().start = min(ret.back().start, i->start);
                    ret.back().end = max(ret.back().end, i->end);
                }
            }
            return ret;
        }
};

 

posted on 2015-06-06 19:28  承续缘  阅读(250)  评论(0编辑  收藏  举报

导航