057 Insert Intervals

Source: Internet
Author: User

Title: 057 Insert Intervals

This problem is to consider whether the two boundary points will fall into the original intervals within a certain interval or outside, the discussion can be divided into the situation

classSolution:#@param intervals, a list of intervals    #@param newinterval, a Interval    #@return A list of Interval    definsert (self, intervals, newinterval): Intervals.insert (0, Interval (-1001,-1000)) Intervals.append (Interval (100000,100001)) In_head=False In_tail=False i= 1Start=Newinterval.start End=Newinterval.end whileI <len (intervals):ifIntervals[i-1].start <= Start andStart <Intervals[i].start:head= I-1ifStart <= intervals[i-1].end:in_head=TrueifIntervals[i-1].end < End andEnd <=Intervals[i].end:tail=IifEnd >=Intervals[i].start:in_tail=True i+ = 1ifIn_head andIn_tail:intervals[tail].start=Intervals[head].start Intervals[head:tail]= []        ifIn_head and  notIn_tail:intervals[head].end=End Intervals[head+1:tail] = []        if  notIn_head andIn_tail:intervals[tail].start=Start Intervals[head+1:tail] = []        if  notIn_head and  notIn_tail:intervals[head+1:tail] =[] Intervals.insert (head+1, Interval (start,end))returnINTERVALS[1:-1]

057 Insert Intervals

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.