LeetCode: Insert Interval [056]

【题目】

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

【题意】

给定一个不重叠区间集合,集合按区间的起始值已经排好序,插入一个区间,如果插入的区间与某些区间重叠,则合并

【思路】

依次扫描集合中的各个区间,和待插入集合比较,确定待插入集合的位置或新合并集合的起始值。

关键在于比较:集合中每个区间interval[start, end]和insertInterval[start, end]有以下几种可能的位置关系

1. interval在insertInterval的左边,即interval.end<insertInterval.start

2. Interval的右边界在insertInterval区间内,而左边界还在InsertInterval区间外,即Interval.start<insertInterval.start && Interval.end>=insertInterval.start && Interval.end<=insertInterval.end;

3. Interval区间覆盖了insertInterval区间,即Interval.start<InsertInterval.start&&Interval.end>InsertInterval.end

4. Interval区间被InsertInterval区间覆盖,即interval.start>=insertInterval.start&&interval.end<InsertInterval.end

5. Interval左边界在InsertInterval区间内,而右边界还在InsertInterval区间外,即Interval.start>=InsertInterval.start && Interval.end<=InsertInterval.end && interval.end>InsertInterval.end

6. Interval在InsertInterval的右边,即Interval.start>InsertInterval.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) {}
 * };
 */
class Solution {
public:
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        vector<Interval>result;
        int size=intervals.size();
        bool hasInserted=false;     //是否已经插入
        for(int i=0; i<size; i++){
            if(hasInserted){
                //如果新区间已经插入,则区间集合中的后续集合直接添加到result集合中即可
                result.push_back(intervals[i]);
                continue;
            }

            if(intervals[i].start<newInterval.start){
                if(intervals[i].end<newInterval.start){
                    //在新区间的左侧
                    result.push_back(intervals[i]);
                }
                else if(intervals[i].end<=newInterval.end){
                    //右边界新区间内,也即存在重合,更新新区间的左边界
                    newInterval.start=intervals[i].start;
                }
                else{
                    //当前区间覆盖了新区间
                    result.push_back(intervals[i]);
                    hasInserted=true;
                }
            }
            else{
                if(intervals[i].start>newInterval.end){
                    //在新区间的右边,插入新区间,及当前区间  【注意,这种情况下需要插入两个区间】
                    result.push_back(newInterval);
                    result.push_back(intervals[i]);
                    hasInserted=true;
                }
                else if(intervals[i].end<=newInterval.end){
                    //当前区间被新区间包含
                    continue;
                }
                else{
                    //左边界在新区间内,也即存在重合,更新新区间的右边界
                    newInterval.end=intervals[i].end;
                }
            }
        }
        if(!hasInserted){
            result.push_back(newInterval);
        }
        return result;
    }
};

LeetCode: Insert Interval [056]

时间: 2024-08-28 02:37:06

LeetCode: Insert Interval [056]的相关文章

[leetcode]Insert Interval @ Python

原题地址:https://oj.leetcode.com/problems/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

2015.04.01 Leetcode Insert interval

Insert Interval解法: 先用start,end两个变量来定位 newinterval可能要插入的起始点和结束点. 分三种情况来看是否需要调整或合并(merge). case1: 在start 之前直接copy 到结果res: case2: 在end之后也是直接copy到res中; case3: 需要合并的就是newInterval.start, newInterval.end, 和任选的一个interval 的interval.start , interval.end 四个边界的比

LeetCode Insert Interval

原题链接在这里:https://leetcode.com/problems/insert-interval/ AC Java: 1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8

[LeetCode] 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]Insert Interval 考虑多种情况

写太复杂了. 思想:确定带插入区间的每个边界位于给定区间中的哪个位置,共有5种情况 -1 |(0)_1_(2)|  (3) 其中,0,1,2这三种情况是一样的. 确定每个带插入区间的两个边界分别属于哪种情况,记为flag0和flag1. 然后根据flag0和flag1的组合情况,分9种情况进行讨论 class Solution { public: vector<Interval> insert(vector<Interval> &its, Interval ni) { in

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刷题笔记】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][JavaScript]Insert Interval

https://leetcode.com/problems/insert-interval/ 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 t

Insert Interval leetcode java

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