Java for Leetcode 056 Merge intervals

Source: Internet
Author: User

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18] ,
Return [1,6],[8,10],[15,18] .

Problem solving idea One:

Using two pointers startindex and endindex to maintain the position of the start and end of each add intervals, and then categorize the discussion, the Java implementation is as follows:

Public list<interval> Merge (list<interval> intervals) {list<interval> List = new Arraylist<in Terval> (); if (intervals.size () = = 0) return List;list.add (Intervals.get (0)); for (int i = 1; i < intervals.size (); i++ ) {Interval temp = intervals.get (i); int startIndex = 0, EndIndex = 0;for (int j = 0; J < List.size (), j + +) {if (temp.st Art > List.get (j). End) {StartIndex + = 2;endindex + 2;continue;} if (Temp.end < List.get (j). Start) break;if (Temp.start >= List.get (j). Start) startindex++;if (Temp.end > List.get (j). End) {EndIndex + = 2;continue;} if (Temp.end >= list.get (j). Start) Endindex++;break;} if (startindex==endindex&&startindex%2==0) List.add (startindex/2,new Interval (temp.start,temp.end)); else if (startindex%2==0&&endindex%2==0) {list.get (STARTINDEX/2). Start=temp.start;list.get (STARTINDEX/2). end= temp.end;for (int k=1;k<endindex/2-startindex/2;k++) list.remove (startindex/2+1);} else if (startindex%2==0&&endindex%2!=0) {LIST.GET (STARTINDEX/2). Start=temp.start;list.get (STARTINDEX/2). End=list.get (ENDINDEX/2). end;for (int k=1;k<= endindex/2-startindex/2;k++) List.remove (startindex/2+1);} else if (startindex%2!=0&&endindex%2==0) {list.get (STARTINDEX/2). end=temp.end;for (int k=1;k<endindex/2- startindex/2;k++) List.remove (startindex/2+1);} else if (startindex%2!=0&&endindex%2!=0) {list.get (STARTINDEX/2). End=list.get (ENDINDEX/2). end;for (int k=1; k<=endindex/2-startindex/2;k++) List.remove (startindex/2+1);}} return list;}

Two ways to solve problems:

First, a comparator is constructed, the interval is sorted by start, and then traversed, and if the result set is empty or interval does not overlap with the last one in the result set, the current is directly interval interval added to the result set. If an overlap occurs, the last right endpoint of the result collection is changed to the interval current interval right endpoint, and the Java implementation is as follows:

Public list<interval> Merge (list<interval> intervals) {        list<interval> List = new arraylist< Interval> ();        comparator<interval> Comparator = new comparator<interval> () {            @Override public            int Compare ( 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)                 L Ist.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 056 Merge 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.