Leetcode–refresh–insert Inverval

Source: Internet
Author: User

Similar to merge intervals. Easier than merge interval, because every side is kind of "sorted".

1 /**2 * Definition for an interval.3 * struct Interval {4 * int start;5 * int end;6 * Interval (): Start (0), end (0) {}7 * Interval (int s, int e): Start (s), End (e) {}8  * };9  */Ten classSolution { One  Public: Avector<interval> Insert (vector<interval> &intervals, Interval newinterval) { -Vector<interval>result; -         if(intervals.size () = =0) { the Result.push_back (newinterval); -             returnresult; -         } -queue<int>Left , right; +         intLen =intervals.size (); -         BOOLLF =true, RF =true; +          for(inti =0; i < Len; i++) { A             if(LF && Newinterval.start <Intervals[i].start) { at Left.push (newinterval.start); -LF =false; -             } -             if(RF && Newinterval.end <intervals[i].end) { - Right.push (newinterval.end); -RF =false; in             } - Left.push (intervals[i].start); to Right.push (intervals[i].end); +         } -         if(LF) Left.push (newinterval.start); the         if(RF) Right.push (newinterval.end); *          while(!Left.empty ()) { $             ints = Left.front (), E =Right.front ();Panax Notoginseng Left.pop (), Right.pop (); -              while(!Left.empty ()) { the                 if(e >=Left.front ()) { +E =Right.front (); A Left.pop (), Right.pop (); the}Else  Break; +             } - Result.push_back (Interval (S, e)); $         } $         returnresult; -     } -};

Leetcode–refresh–insert Inverval

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.