LeetCode Insert Interval

class Solution {
public:
    // verbose one
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        vector<Interval> ret;
        int ns = newInterval.start;
        int ne = newInterval.end;
        int num = intervals.size();
        int i, is, ie;

        for (i = 0; i<num; i++) {
            if (intervals[i].end >= ns) break;
            ret.push_back(intervals[i]);
        }
        is = i;
        bool dispart = false;
        if (is == num || (dispart = intervals[is].start > ne)) {
            ret.push_back(Interval(ns, ne));
        }
        
        for (; i<num; i++) {
            if (intervals[i].start > ne) break;
        }
        ie = i - 1;
        if (is != num && ie >= is) {
            int s, e;
            if (dispart) {
                s = intervals[is].start;
            } else {
                s = min(intervals[is].start, ns);
            }
            e = max(intervals[ie].end, ne);
            ret.push_back(Interval(s, e));
        }
        while(i < num) ret.push_back(intervals[i++]);
        
        return ret;
    }
    // better to read and comprehend
    vector<Interval> _insert(vector<Interval> &intervals, Interval newInterval) {
        vector<Interval> ret;
        int ns = newInterval.start;
        int ne = newInterval.end;
        int num = intervals.size();
        
        bool joined = false;
        bool pushed = false;
        for (int i=0; i<num; i++) {
            Interval cur = intervals[i];
            if (!is_overlapped(cur, newInterval)) {
                if (newInterval.end < cur.start) {
                    if (!pushed) {
                        ret.push_back(newInterval);
                        pushed = true;
                    }
                }
                ret.push_back(cur);
            } else {
                newInterval = join(newInterval, cur);
                joined = true;
            }
        }
        if ((!joined&&!pushed) || !pushed) {
            ret.push_back(newInterval);
        }
        return ret;
    }

    bool is_overlapped(Interval& a,  Interval& b) {
        if (a.end < b.start || b.end < a.start) return false;
        return true;
    }
    
    Interval join(Interval& a,  Interval& b) {
        return Interval(min(a.start, b.start), max(a.end, b.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) {}
 * };
 */
 // 22:20
class Solution {
public:
    vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
        vector<Interval> res;
        int len = intervals.size();
        int pos = 0;
        while (pos < len && intervals[pos].end < newInterval.start) {
            res.push_back(intervals[pos]);
            pos++;
        }
        
        while (pos < len && newInterval.end >= intervals[pos].start) {
            newInterval.start = min(newInterval.start, intervals[pos].start);
            newInterval.end = max(newInterval.end, intervals[pos].end);
            pos++;
        }
        res.push_back(newInterval);
        
        while (pos < len) {
            res.push_back(intervals[pos++]);
        }
        return res;
    }
};

不过时间要500+ms还是让人费解,而且在时间分布图上python居然遥遥领先

又做了一次感觉已经能自己想出来了。

posted @ 2014-03-24 18:50  卖程序的小歪  阅读(194)  评论(0编辑  收藏  举报