#Leetcode# 57. Insert Interval

https://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.

Example 1:

Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]

Example 2:

Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].

代码:

/**
 * 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) {
        int n = intervals.size();
        vector<Interval> ans;
        if(n == 0) {
            ans.push_back(newInterval);
            return ans;
        }
        
        int cnt = 0;
        if(n == 1) {
            if(!isintersect(intervals[0].start, intervals[0].end, newInterval.start, newInterval.end)) {
                if(intervals[0].start < newInterval.start) {
                    ans.push_back(intervals[0]);
                    ans.push_back(newInterval);
                } else {
                    ans.push_back(newInterval);
                    ans.push_back(intervals[0]);
                }
            } else {
                Interval now;
                now.start = min(intervals[0].start, newInterval.start);
                now.end = max(intervals[0].end, newInterval.end);
                
                ans.push_back(now);
            }
            return ans;
        }
        
        for(int i = 0; i < n; i ++) {
            if(!isintersect(intervals[i].start, intervals[i].end, newInterval.start, newInterval.end))
                ans.push_back(intervals[i]), cnt ++;
            else {
                Interval node;
                node.start = min(intervals[i].start, newInterval.start);
                node.end = max(intervals[i].end, newInterval.end);
                bool flag = false;
                while(i < n && isintersect(intervals[i].start, intervals[i].end, newInterval.start, newInterval.end))
                    flag = true, i ++;
                if(flag) i -= 1;
                node.start = min(intervals[i].start, node.start);
                node.end = max(intervals[i].end, node.end);
                
                ans.push_back(node);
            }
        }
        
        int temp = -1;
        if(cnt == n) {
           for(int i = 0; i < n; i ++) {
                if(newInterval.start > intervals[i].start) temp = i;
            }
            //if(temp == -1) temp = 0;
            vector<Interval> anss = ans;
            ans.clear();
            if(temp == -1)
                ans.push_back(newInterval);
            for(int i = 0; i < anss.size(); i ++) {
                ans.push_back(anss[i]);
                if(i == temp) ans.push_back(newInterval);
            }
            
        } 
        
        return ans;
        
    }
    bool isintersect(int x1, int y1, int x2, int y2) {
        if(y1 < x2 || x1 > y2)
            return false;
        return true;
    }
};

  区间合并 因为考虑的情况不完全 各种 WA  最后 AC 的时候老泪纵横了

 

posted @ 2019-04-09 10:31  丧心病狂工科女  阅读(183)  评论(0编辑  收藏  举报