Insert Interval Array

原创 2015年07月06日 21:47:29

思路:

方法一:
类似上一题思想,不断合并,标记,最后在排序,写的很烂。
Runtime Error
原因应该是爆栈了。

void quick_sort(vector<Interval> &array, int left, int right) {
    if(left < right) {
        int i = left, j = right;
        Interval x = array[i];
        while(i < j) {
            while(i < j && array[j].start >= x.start) {
                --j;
            }
            if(i < j) {
                swap(array[i], array[j]);
                ++i;
            }
            while(i < j && array[i].start < x.start) {
                ++i;
            }
            if(i < j) {
                swap(array[i], array[j]);
                --j;
            }
        }
        array[i] = x;
        quick_sort(array, left, i - 1);
        quick_sort(array, i + 1, right);
    }
}
void clear(Interval &x) {
    x.start = 1;
    x.end = 0;
}
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
    int n = intervals.size();
    vector<Interval> ans;
    for(int i = 0; i < n; ++i) {
        if(newInterval.start > intervals[i].end || newInterval.end < intervals[i].start) {
            continue;
        }
        if(newInterval.start == intervals[i].end) {
            newInterval.start = intervals[i].start;
            clear(intervals[i]);
        }else if(newInterval.end == intervals[i].start) {
            newInterval.end = intervals[i].end;
            clear(intervals[i]);
        }else {
            newInterval.start = min(newInterval.start, intervals[i].start);
            newInterval.end = max(newInterval.end, intervals[i].end);
            clear(intervals[i]);
        }
    }
    //delete
    for(int i = 0; i < n; ++i) {
        if(!(intervals[i].start == 1 && intervals[i].end == 0)) {
            ans.push_back(intervals[i]);
        }
    }
    ans.push_back(newInterval);
    show(ans);
    quick_sort(ans, 0, n - 1);
    return ans;
}

要利用好原array已经排序好的事实。

方法二:TLE。

看到没pass的数据为:

intervals : [1,2],[2,3],[3,4],[4,5],....,[19999,20000]
newInterval : [0,20001]

这样就会一直走if…else…的第三个分支,会不停的erase数组。

时间复杂度O(N),空间复杂度O(1)。

/**
 * 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>::iterator it = intervals.begin();
        while(it != intervals.end()) {
            if(newInterval.end < it->start) {
                intervals.insert(it, newInterval);
                return intervals;
            }else if(newInterval.start > it->end) {
                it++;
                continue;
            }else {
                newInterval.start = min(it->start, newInterval.start);
                newInterval.end = max(it->end, newInterval.end);
                it = intervals.erase(it);
            }
        }
        intervals.insert(intervals.end(), newInterval);
        return intervals;
    }
};

方法三:

相比方法二,它用空间换时间。如果遇到上一方法没pass的数据集,只记录变化,最后才往结果集里加入。

/**
 * 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> res;
        int n = intervals.size();
        for(int i = 0; i < n; ++i) {
            if(newInterval.end < intervals[i].start) {
                res.push_back(newInterval);
                res.insert(res.end(), intervals.begin() + i, intervals.end());
                return res;
            }else if(newInterval.start > intervals[i].end) {
                res.push_back(intervals[i]);
            }else {
                newInterval.start = min(newInterval.start, intervals[i].start);
                newInterval.end = max(newInterval.end, intervals[i].end);
            }
        }
        res.push_back(newInterval);
        return res;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【leetcode】Array——Insert Interval(57)

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

Leetcode-Array-57(insert Interval)

当然,leetcode拥有良好的函数处理平台,但有时候我想知道如何编写出来一个完整的程序,现在我给出一种编写方法吧,请各位大神多多指教

Array(10) -- Insert Interval, Merge Intervals, Trapping Rain Water, Next Permutation

Insert Interval, Merge Intervals, Trapping Rain Water, Next Permutation

LeetCode[Array]: Insert Interval

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

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_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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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