zoukankan      html  css  js  c++  java
  • 56. Merge Intervals 57. Insert Interval *HARD*

    1. Merge

    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 compare(Interval a, Interval b)
        {
            if(a.start != b.start)
                return a.start < b.start;
            return a.end < b.end;
        }
        vector<Interval> merge(vector<Interval>& intervals) {
            sort(intervals.begin(), intervals.end(), compare);
            vector<Interval> v;
            int n = intervals.size(), i;
            if(n<1)
                return v;
            for(i = 0; i < n; i++)
            {
                if(v.size() && intervals[i].start <= v[v.size()-1].end)
                    v[v.size()-1].end = max(intervals[i].end, v[v.size()-1].end);
                else
                    v.push_back(intervals[i]);
            }
            return v;
        }
    };

    2. Insert

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

    (1) 用上面的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)
        {
            if(a.start != b.start)
                return a.start < b.start;
            return a.end < b.end;
        }
        vector<Interval> merge(vector<Interval>& intervals) {
            sort(intervals.begin(), intervals.end(), compare);
            vector<Interval> v;
            int n = intervals.size(), i;
            if(n<1)
                return v;
            for(i = 0; i < n; i++)
            {
                if(v.size() && intervals[i].start <= v[v.size()-1].end)
                    v[v.size()-1].end = max(intervals[i].end, v[v.size()-1].end);
                else
                    v.push_back(intervals[i]);
            }
            return v;
        }
        vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
            intervals.push_back(newInterval);
            return merge(intervals);
        }
    };

    (2)

    /**
     * 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) {
            int n = intervals.size(), l, i;
            vector<Interval> v;
            for(i = 0; i < n; i++)
            {
                if(newInterval.start > intervals[i].end)
                    v.push_back(intervals[i]);
                else
                    break;
            }
            if(i >= n)
            {
                v.push_back(newInterval);
                return v;
            }
            l = v.size();
            v.push_back(intervals[i]);
            if(newInterval.start < v[l].start)
                v[l].start = newInterval.start;
            while(i < n && newInterval.end > intervals[i].end)
                i++;
            if(i<n && newInterval.end >= intervals[i].start)
                v[l].end = intervals[i++].end;
            else
                v[l].end = newInterval.end;
            while(i<n)
                v.push_back(intervals[i++]);
            return v;
        }
    };
  • 相关阅读:
    查看详细linux系统信息的命令和方法
    linux下将当前目录下的文件名存到一个文本文件里
    详解linux下批量替换文件内容的三种方法(perl,sed,shell)
    将二维数组中某个值为空的数组进行删除!
    字符串截取,对数字,英文,汉字都可以
    根据二维数组的某列数值来对二维数组进行排序
    iOS开发之第三方分享QQ分享,史上最新最全第三方分享QQ方式实现
    iOS开发之第三方登录微博-- 史上最全最新第三方登录微博方式实现
    iOS开发之第三方登录微信-- 史上最全最新第三方登录微信方式实现
    iOS开发之第三方登录QQ -- 史上最全最新第三方登录QQ方式实现
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5266607.html
Copyright © 2011-2022 走看看