[Easy] Insertion interval

Source: Internet
Author: User

Title Source: http://www.lintcode.com/zh-cn/problem/insert-interval/

You can accept the following procedures:

1 /**2 * Definition of Interval:3 * Class Interval {4 * Public:5 * int start, end;6 * Interval (int start, int end) {7 * This->start = start;8 * this->end = end;9  *     }Ten  * } One  */ A BOOLComp (Interval A, Interval b) { -     returnA.start <B.start; - } the  - classSolution { -  Public: -     /** + * Insert newinterval into intervals. - * @param intervals:sorted interval list. + * @param newinterval:new interval. A * @return: A new interval list. at      */ -vector<interval> Insert (vector<interval> &intervals, Interval newinterval) { -         //Write your code here - Intervals.insert (Intervals.end (), newinterval); - sort (Intervals.begin (), Intervals.end (), comp); -             intLen =intervals.size (); inVector<interval>Res; -             inti; to              for(i =1; i < Len; ++i) { +                  while(Intervals[i].start <= intervals[i-1].end && I <Len) { -Intervals[i].start = intervals[i-1].start; theIntervals[i].end = intervals[i].end > intervals[i-1].end? *intervals[i].end:intervals[i-1].end; $i++;Panax Notoginseng                 } -Res.push_back (intervals[i-1]); the                 if(I >=Len) { +                     returnRes; A                 } the             } +Res.push_back (intervals[i-1]); -             returnRes; $     } $};

The complete test procedure is as follows:

1#include <iostream>2#include <string>3#include <vector>4#include <algorithm>5 using namespacestd;6 7 classInterval {8  Public:9     intstart, end;TenInterval (intStartintend) { One          This->start =start; A          This->end =end; -     } - }; the  - BOOLComp (Interval A, Interval b) { -     returnA.start <B.start; - } +  - classSolution { +  Public: A     /** at * Insert newinterval into intervals. - * @param intervals:sorted interval list. - * @param newinterval:new interval. - * @return: A new interval list. -      */ -vector<interval> Insert (vector<interval> &intervals, Interval newinterval) { in Intervals.insert (Intervals.end (), newinterval); - sort (Intervals.begin (), Intervals.end (), comp); to             intLen =intervals.size (); +Vector<interval>Res; -             inti; the              for(i =1; i < Len; ++i) { *                  while(I < len && Intervals[i].start <= intervals[i-1].end) { $Intervals[i].start = intervals[i-1].start;Panax NotoginsengIntervals[i].end = intervals[i].end > intervals[i-1].end? -intervals[i].end:intervals[i-1].end; thei++; +                 } ARes.push_back (intervals[i-1]); the                 if(I >=Len) { +                     returnRes; -                 } $             } $Res.push_back (intervals[i-1]); -             returnRes; -     } the }; - Wuyi intMain () the { - solution Solu; WuInterval I (1,2); -Interval J (5,9); About     //Interval K (3,4);//[1,2],[3,4],[5,9] $     //Interval K (2,5);//[1,9] -     //Interval K (2,4);//[1,4],[5,9] -     //Interval K (3,5);//[1,2],[3,9] -Interval K (0,4);//[0,4],[5,9] AVector<interval>A; +Vector<interval>b; the A.push_back (i); - A.push_back (j); $b=Solu.insert (a,k); the      for(intI=0; I<b.size (); i++) thecout<<b[i].start<<","<<b[i].end<<Endl; the}

[Easy] Insertion interval

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.