• 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;
        }
    };
  • 相关阅读:
    git添加本地项目到git
    GitLab项目迁移到Gerrit
    flask一些资料
    openldap sshkey & 用户自定义属性
    openldap复制
    openldap主机访问控制(基于用户组)
    openldap主机访问控制(基于ip)
    openldap自定义schema
    openldap主机访问控制(基于hostname)
    openldap权限sudo
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5266607.html
Copyright © 2020-2023  润新知