Java for Leetcode 057 Insert Interval

Source: Internet
Author: User

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

Assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9] , insert and merge in as [2,5] [1,5],[6,9] .

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

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

Problem solving idea One:

Refer to Java for leetcode 056 Merge intervals idea one, remove the outermost loop, Java implementation is as follows:

Public list<interval> Insert (list<interval> intervals, Interval newinterval) {if (NewInterval = = null)            return intervals;            int startIndex = 0, endIndex = 0;                    for (int j = 0; J < Intervals.size (); j + +) {if (Newinterval.start > Intervals.get (j). End) {                    StartIndex + = 2;                    EndIndex + = 2;                Continue                } if (Newinterval.end < Intervals.get (j). Start) break;                if (Newinterval.start >= intervals.get (j). Start) startindex++;                    if (Newinterval.end > Intervals.get (j). End) {EndIndex + = 2;                Continue                } if (Newinterval.end >= intervals.get (j). Start) endindex++;            Break } if (startindex==endindex&&startindex%2==0) Intervals.add (startinDex/2,new Interval (newinterval.start,newinterval.end));                else if (startindex%2==0&&endindex%2==0) {intervals.get (STARTINDEX/2). Start=newinterval.start;                Intervals.get (STARTINDEX/2). End=newinterval.end;            for (int k=1;k<endindex/2-startindex/2;k++) intervals.remove (startindex/2+1); } else if (startindex%2==0&&endindex%2!=0) {intervals.get (STARTINDEX/2). Start=newinterval                . Start;                Intervals.get (STARTINDEX/2). End=intervals.get (ENDINDEX/2). end;            for (int k=1;k<=endindex/2-startindex/2;k++) intervals.remove (startindex/2+1); } else if (startindex%2!=0&&endindex%2==0) {intervals.get (STARTINDEX/2). END=NEWINTERVAL.E                nd            for (int k=1;k<endindex/2-startindex/2;k++) intervals.remove (startindex/2+1); } else if (startindex%2!=0&&endindex%2!=0) {intervals.get (STARTINDEX/2). End=intervals.get (ENDINDEX/2). end;            for (int k=1;k<=endindex/2-startindex/2;k++) intervals.remove (startindex/2+1);    } return intervals; }

Two ways to solve problems:

Refer to Java for leetcode 056 Merge intervals idea two, add and reorder, Java implementation is as follows:

Public list<interval> Insert (list<interval> intervals, Interval newinterval) {if (intervals = = null)        return intervals;        list<interval> list = new arraylist<interval> ();        Intervals.add (NewInterval); comparator<interval> Comparator = new comparator<interval> () {@Override public int comp                Is (Interval O1, Interval O2) {if (O1.start = = O2.start) return o1.end-o2.end;            return o1.start-o2.start;        }        };        Collections.sort (intervals, comparator); for (Interval interval:intervals) {if (list.size () = = 0 | | list.get (list.size ()-1). End < Interval.start            ) List.add (New Interval (Interval.start, interval.end));        Else List.get (List.size ()-1). End = Math.max (Interval.end, List.get (List.size ()-1). end);    } return list; }

Java for Leetcode 057 Insert 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.