zoukankan      html  css  js  c++  java
  • 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;
    		}
        }
    };


  • 相关阅读:
    请注意更新TensorFlow 2.0的旧代码
    tf.cast用法
    文件句柄
    Python学习(四)cPickle的用法
    机器都会学习了,你的神经网络还跑不动?来看看这些建议
    Hadoop集群管理之配置文件
    SQL之case when then用法
    关于2014
    Oracle之虚拟索引
    Linux之Ganglia源码安装
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3343301.html
Copyright © 2011-2022 走看看