[Leetcode] Insert Interval

Source: Internet
Author: User

Given a setNon-overlappingIntervals, 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[1,5],[6,9].

Example 2:Given[1,2],[3,5],[6,7],[8,10],[12,16], Insert and merge[4,9]In[1,2],[3,10],[12,16].

This is because the new interval[4,9]Overlaps[3,5],[6,7],[8,10].

Pay attention to the sorting between various if and corresponding else if and the case where they cannot be included!

Class solution {public: vector <interval> insert (vector <interval> & intervals, interval newinterval) {vector <interval> result; interval temp; int Len = intervals. size (); If (LEN = 0) {// in special cases, if intervals is null, the result is directly returned. push_back (newinterval); return result;} int startindex, endindex; int I, startflag = 0, endflag = 0; for (I = 0; I <Len ;) {If (intervals [I]. start <= newinterval. start & intervals [I]. end> = newinte Rval. end) {// return intervals;} else if (startflag = 0 & intervals [I]. start> newinterval. end) {// The new area is out of all intervals and is returned before a certain interval. push_back (newinterval); startflag = 1; endflag = 1; break;} else if (intervals [I]. end <newinterval. start & I! = Len-1) {// the starting point of the new area after this interval result. push_back (intervals [I]); I ++;} else if (startflag = 0 & newinterval. start <intervals [I]. start & newinterval. end> = intervals [I]. start & newinterval. end <= intervals [I]. end) {// the start point of the new area before this interval, startflag = 1; endflag = 1; temp. start = newinterval. start; temp. end = intervals [I]. end; result. push_back (temp); I ++; break;} else if (startflag = 0 & newinterval. start <intervals [I]. start & newinterval. end> intervals [I]. end) {// the start point of the new area before this interval. The end point exceeds this interval startflag = 1; temp. start = newinterval. start;} else if (startflag = 0 & newinterval. start> = intervals [I]. start & newinterval. start <= intervals [I]. end & newinterval. end> intervals [I]. end) {// the start point of the new area between these intervals. The end point exceeds this interval startflag = 1; temp. start = intervals [I]. start;} else if (startflag = 0 & I = len-1 & intervals [I]. end <newinterval. start) {// the result of the new area out of the last interval. push_back (intervals [I]); result. push_back (newinterval); return result;} If (startflag = 1 & endflag = 0 & I = len-1 & newinterval. end> intervals [I]. end) {// The End of the new area is after the last interval temp. end = newinterval. end; endflag = 1; I ++; result. push_back (temp); return result;} else if (startflag = 1 & endflag = 0 & newinterval. end> intervals [I]. end) {// the end point of the new area crosses the I ++; continue;} else if (startflag = 1 & endflag = 0 & newinterval. end <intervals [I]. start) {// endpoint of the new area before this interval endflag = 1; temp. end = newinterval. end; result. push_back (temp); break;} else if (startflag = 1 & endflag = 0 & newinterval. end> = intervals [I]. start & newinterval. end <= intervals [I]. end) {// endflag = 1; temp. end = intervals [I]. end; I ++; result. push_back (temp); break ;}// end for while (I <Len) {result. push_back (intervals [I ++]);} return result;} // end func };

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.