当前位置:编程学习 > C/C++ >>

leetcode_question_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 [2,5] in as [1,5],[6,9].
 
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
 
This is because the new interval [4,9] 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) {  
        // Start typing your C/C++ solution below  
        // DO NOT write int main() function  
        vector<Interval>::iterator itstart=intervals.begin();  
        for(; itstart!=intervals.end(); ++itstart)  
            if((*itstart).start <= newInterval.start && newInterval.start <= (*itstart).end)  
                break;  
        vector<Interval>::iterator itend=intervals.begin();  
        for(; itend!=intervals.end(); ++itend)  
            if((*itend).start <= newInterval.end && newInterval.end <= (*itend).end)  
                break;  
        if(itstart!=intervals.end() && itend!=intervals.end())  
        {  
            if(itstart != itend)  
            {  
                (*itstart).end = (*itend).end;  
                int tmp = (*itstart).end;  
                itstart++;  
                while(itstart != intervals.end())  
                if((*itstart).start <= tmp)  
                    itstart = intervals.erase(itstart);  
                else break;  
            }  
            return intervals;  
        }else if(itstart==intervals.end() && itend!=intervals.end())  
        {  
            (*itend).start = newInterval.start;  
            int tmp1 = (*itend).start;  
            int tmp2 = (*itend).end;  
            itstart = intervals.begin();  
            while((*itstart).start != tmp1 && (*itstart).end != tmp2)  
                if((*itstart).start >= tmp1 && (*itstart).end <= tmp2)  
                     itstart = intervals.erase(itstart);  
                else ++itstart;  
            return intervals;  
        }else if(itstart!=intervals.end() && itend==intervals.end())  
        {  
            (*itstart).end = newInterval.end;  
            int tmp = newInterval.end;  
            ++itstart;  
            while(itstart != intervals.end())  
            if((*itstart).start < tmp)  
                itstart = intervals.erase(itstart);  
            else break;  
            return intervals;  
        }else{  
            vector<Interval>::iterator it=intervals.begin();  
            for(; it!=intervals.end(); ++it)  
                if((*it).start > newInterval.start)  
                    break;  
            //--it;  
            vector<Interval>::iterator it1 = intervals.insert(it,newInterval);  
            while(it1 != intervals.end())  
            {    
                if((*it1).end < newInterval.end)  
                it1 = intervals.erase(it1);  
                else  
                it1++;  
            }  
            return intervals;  
        }  
    }  
};  

 


补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,