[LeetCode]Insert Interval 考虑多种情况

写太复杂了。

思想:确定带插入区间的每个边界位于给定区间中的哪个位置,共有5种情况

-1 |(0)_1_(2)|  (3)

其中,0,1,2这三种情况是一样的。

确定每个带插入区间的两个边界分别属于哪种情况,记为flag0和flag1。

然后根据flag0和flag1的组合情况,分9种情况进行讨论

class Solution {
public:
    vector<Interval> insert(vector<Interval> &its, Interval ni) {
        int i,n=its.size(),j,k;
        int flag0=-1,flag1=-1;
        vector<Interval>ans;
        if(n==0){
            ans.push_back(ni);
            return ans;
        }
        for(i=0;i<n;++i)
			if((i==0||its[i-1].end<ni.start)&&its[i].start>ni.end)
				break;
        if(i<n||(i==n&&its[n-1].end<ni.start)){
            for(k=0;k<i;++k)ans.push_back(its[k]);
            ans.push_back(ni);
            for(;k<n;++k)ans.push_back(its[k]);
            return ans;
        }
        for(i=0;i<n;++i){
			if(its[i].end==ni.start){flag0=2;break;}
			else if(its[i].start==ni.start){flag0=0;break;}
            else if(its[i].start>ni.start)break;
            else if(its[i].start<ni.start&&its[i].end>ni.start){flag0=1;break;}
        }
        if(i==n&&flag0==-1)flag0=3;//no exist
        for(j=i;j<n;++j){
			if(its[j].end==ni.end){flag1=2;break;}
			else if(its[j].start==ni.end){flag1=0;break;}
            else if(its[j].start>ni.end)break;
            else if(its[j].start<ni.end&&its[j].end>ni.end){flag1=1;break;}
        }
        if(j==n&&flag1==-1)flag1=3;//可能存在
        for(k=0;k<i;++k)ans.push_back(its[k]);
        if(flag1==-1){
            if(flag0==3){
                ans.push_back(ni);
                return ans;
            }
            else if(flag0>=0&&flag0<=2){
                its[i].end=ni.end;
            }
            else if(flag0==-1){
                its[i].end=ni.end;its[i].start=ni.start;
            }
			ans.push_back(its[i]);
            for(k=j;k<n;++k)ans.push_back(its[k]);
        }
        else if(flag1>=0&&flag1<=2){
            if(flag0==3){
                its[j].start=ni.start;
            }
            else if(flag0>=0&&flag0<=2){
                if(i!=j){
                    its[i].end=its[j].end;
                    ans.push_back(its[i]);
                    for(k=j+1;k<n;++k)ans.push_back(its[k]);
                    return ans;
                }
            }
            else if(flag0==-1){
                if(i==j){
                    its[j].start=ni.start;
                }
                else{
                    its[i].start=ni.start;
                    its[i].end=its[j].end;
                    ans.push_back(its[i]);
                    for(k=j+1;k<n;++k)ans.push_back(its[k]);
                    return ans;
                }
            }
            for(k=i;k<n;++k)ans.push_back(its[k]);
        }
        else if(flag1==3){//j==n
            if(flag0==3){
                ans.push_back(Interval(-1117,-1117));//不存在
            }
            else if(flag0>=0&&flag0<=2){
                its[i].end=ni.end;
				ans.push_back(its[i]);
				return ans;
            }
            else if(flag0==-1){
                its[i].start=ni.start;
                its[i].end=ni.end;
				ans.push_back(its[i]);return ans;
            }
            for(k=i;k<n;++k)ans.push_back(its[k]);
        }
        return ans;
    }
};
时间: 2024-11-10 11:11:10

[LeetCode]Insert Interval 考虑多种情况的相关文章

LeetCode: Insert Interval [056]

[题目] 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 m

[leetcode]Insert Interval @ Python

原题地址:https://oj.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

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 Insert Interval

原题链接在这里:https://leetcode.com/problems/insert-interval/ AC Java: 1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8

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

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刷题笔记】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][JavaScript]Insert Interval

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 leetcode java

题目: 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 me