• 【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;
            }
    };
  • 相关阅读:
    hdu 1690
    hdu 2962
    hdu 2923
    hdu 1385
    hdu 1505
    python:获取访问访问时的响应时间
    自动化脚本编写环境部署_win7(RF)
    python操作浏览器及截图小结
    Python + Selenium 实现对页面的指定元素截图(可截长图元素)【转载】
    Linux:DNS服务器搭建及配置
  • 原文地址:https://www.cnblogs.com/xbf9xbf/p/4557153.html
Copyright © 2020-2023  润新知