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]], we get [[1,9]].

Insert [3, 4] into [[1,2], [5,9]], we get [[1,2], [3,4], [5,9]].

Analyse: There are three possibilities for the newInterval and currentInterval: newInterval.end < currentInterval.start, newInterval.start > currentInterval.end, and newInterval and currentInterval intersects.

Runtime: 24ms

 1 /**
 2  * Definition of Interval:
 3  * class Interval {
 4  * public:
 5  *     int start, end;
 6  *     Interval(int start, int end) {
 7  *         this->start = start;
 8  *         this->end = end;
 9  *     }
10  * }
11  */
12 class Solution {
13 public:
14     /**
15      * Insert newInterval into intervals.
16      * @param intervals: Sorted interval list.
17      * @param newInterval: new interval.
18      * @return: A new interval list.
19      */
20     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
21         // write your code here
22         vector<Interval> result;
23         Interval pre = newInterval;
24         for (int i = 0; i < intervals.size(); i++) {
25             if (pre.end < intervals[i].start) {
26                 result.push_back(pre);
27                 pre = intervals[i];
28             }
29             else if (pre.start > intervals[i].end) {
30                 result.push_back(intervals[i]);
31             }
32             else {
33                 pre.start = min(pre.start, intervals[i].start);
34                 pre.end = max(pre.end, intervals[i].end);
35             }
36         }
37         result.push_back(pre);
38         return result;
39     }
40 };
时间: 2024-10-10 08:28:55

Insert Intervals的相关文章

057 Insert Intervals

题目: 057 Insert Intervals 这道题就是要考虑两个边界点是否会落到原有的intervals中的某一个区间之内 还是在外部, 分情况讨论即可 class Solution: # @param intervals, a list of Intervals # @param newInterval, a Interval # @return a list of Interval def insert(self, intervals, newInterval): intervals.

插入区间--Insert Intervals

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 &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 解决思路

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 [

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][Python]56: Merge Intervals

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 56: Merge Intervalshttps://oj.leetcode.com/problems/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]

[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 [

[fb 面经] intervals sweep line

Leetcode 上的题 Merge Intervals Insert Intervals Meeting Rooms Meeting RoomsII Topcoder tutorial: Sweep Line Algorithms some from gitbook about sweep line problems on leetcode: https://robinliu.gitbooks.io/leetcode/content/Sweep_Line.html 面经: 题1: Given