#30 Insert Interval

原创 2016年08月29日 12:31:50

题目描述:

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

题目思路:

这题很容易做着做着就晕了,关键是要条理清楚。步骤如下:

1. 把new interval加入到intervals的array中,然后把整个array的所有区间根据start来sort

2. 遍历这个sort过的区间集合。如果发现本区间和上一个区间有overlap(本区间的start在上一个区间中),那么就合并本区间和上一个区间;然后不断的check下一个区间,如果有overlap就再进行合并,直到没有overlap为止。

3. 把最终的区间写到ans中。

这里loop到最后时,对于最后一个区间需要take care一下。

Mycode(AC = 26ms):

/**
 * Definition of Interval:
 * class Interval {
 * public:
 *     int start, end;
 *     Interval(int start, int end) {
 *         this->start = start;
 *         this->end = end;
 *     }
 * }
 */
class Solution {
public:
    /**
     * Insert newInterval into intervals.
     * @param intervals: Sorted interval list.
     * @param newInterval: new interval.
     * @return: A new interval list.
     */
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        // write your code here
        vector<Interval> ans;
        
        // sort the whole intervals according to start
        auto comp = [](const Interval& a, const Interval& b) {
            return a.start <= b.start;    
        };
        
        intervals.push_back(newInterval);
        sort(intervals.begin(), intervals.end(), comp);
        
        int len = intervals.size();
        for (int i = 1; i < len; i++) {
            // if interval i has overlap with interval i - 1, then
            // merge 2 intervals
            while (i < len && intervals[i].start <= intervals[i - 1].end) {
                intervals[i].start = intervals[i - 1].start;
                intervals[i].end = max(intervals[i].end, intervals[i - 1].end);
                i++;
            }
            ans.push_back(intervals[i - 1]);
            if (i >= len) return ans;
        }
        
        ans.push_back(intervals[len - 1]);
        return ans;
    }
};


版权声明:三千微尘里,吾宁爱与憎

相关文章推荐

30.Insert Interval-插入区间(容易题)

插入区间 1.列表内容给出一个无重叠的按照区间起始端点排序的区间列表。在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。2.样例插入区间[2, 5] 到 ...

LeetCode Insert Interval

Description: Given a set of non-overlapping intervals, insert a new interval into the intervals...

LeetCode(57) Insert Interval

题目Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if neces...

LeetCode之Insert Interval--二分查找

题目描述 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge ...

leetcode第一刷_Insert Interval

这道题的难度跟微软的那道面试题相当。 要在集合中插入一段新的集合,相当于求两个集合的并了。对于新加入一段集合的情况,分为下面几种: 1. 加入段跟原来的完全相交,也即他的起点和终点都在被包含在原来...

【leetcode】Array——Insert Interval(57)

题目:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nece...

LeetCode_57---Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...

[leetcode] 57.Insert Interval

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if ne...

Insert Interval

/** * Definition for an interval. * struct Interval { * int start; * int end; * Int...

Leetcode-Array-57(insert Interval)

当然,leetcode拥有良好的函数处理平台,但有时候我想知道如何编写出来一个完整的程序,现在我给出一种编写方法吧,请各位大神多多指教
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)