60. Insert Interval && Merge Intervals

Insert Interval

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

思路: 因为区间按 start 升序,且无重叠。所以插入区间和每一个元素分三种情况考虑。在左边,在右边(此两种情况直接拿区间出来)或者交叉(则更新插入区间范围)。 利用变量 out 判断新的区间是否已经放入。

/**
 * 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> vec;
        bool out = true;
        for(size_t i = 0; i < intervals.size(); ++i) {
            if(intervals[i].end < newInterval.start) {
                vec.push_back(intervals[i]);
            } else if(intervals[i].start > newInterval.end) {
                if(out) { vec.push_back(newInterval); out = false;}
                vec.push_back(intervals[i]);
            } else {
                newInterval.start = min(newInterval.start, intervals[i].start);
                newInterval.end = max(newInterval.end, intervals[i].end);
            }
        }
        if(out)
            vec.push_back(newInterval);
        return vec;
    }
};

Merge Intervals

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

思路: 先按 start 排序。然后,判断当前区间和前一区间是否重叠。若没重叠,则放入;若重叠,则更新前一区间 end, 舍弃当前区间。

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
bool cmp(Interval a, Interval b) {
    return a.start < b.start;
}
class Solution {
public:
    vector<Interval> merge(vector<Interval> &intervals) {
        sort(intervals.begin(), intervals.end(), cmp);
        vector<Interval> vec;
        for(size_t i = 0; i < intervals.size(); ++i) {
            if(vec.empty()) vec.push_back(intervals[i]);
            else if(intervals[i].start <= vec.back().end)
                vec.back().end = max(intervals[i].end, vec.back().end);
            else vec.push_back(intervals[i]);
        }
        return vec;
    }
};
时间: 2024-10-24 10:52:05

60. Insert Interval && Merge Intervals的相关文章

Insert Interval &amp; Merge Intervals

Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary). Example Insert [2, 5] into [[1,2], [5,9]],

LeetCode 57. Insert Interval / Add Interval (get total covered length)

57. Insert Interval 由于intervals已经有序,不需要排序.本题是要返回一个interval数组,所以并不需要对原数组进行改动. 方法一: 由于intervals有序,我们可以二分找到应该插入的位置,然后merge intervals即可. 时间复杂度 O(n) 方法二: 我们只需要找出overlap的部分,merge即可.别的没有overlap的部分保持不变. class Solution { public: vector<vector<int>> ins

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)

[LeetCode] 57. Insert Interval 解决思路

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 [

【Leetcode】【Hard】Insert Interval

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 [

No.57 Insert Interval

No.57 Insert Interval 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,

No.56 Merge Intervals

No.56 Merge Intervals 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]. 法一:直接复用之前写的insert函数,依次将区间段插入到结果集中 1 #include "stdafx.h" 2 #include <vector> 3

leetcode || 56、 Merge Intervals

problem: 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]. Hide Tags Array Sort 题意:给定数组区间,合并有覆盖或者相邻的区间 thinking: (1)一開始我想到用hash table的方法,开一个总区间跨度的数组.对于有区间覆盖的数

LeetCode57 Insert Interval

题目: 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 mer