Leetcode the Merge intervals-----java

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] .

The problem is to merge all the sets, although hard, but it is better to think out, is to rewrite the compare, and then to all the collection of sorting, sorted after the comparison for a long time OK.

[A, b] and [c,d]

In order to compare the size of B and C, if B is large, then merge two sets, remove D and compare to the next set.

If C is large, then add the set directly into result.

Make a final judgment (whether the last set is added to result).

But the first commit to the compare definition has an error.

/*** Definition for a interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0; } * Interval (int s, int e) {start = s; end = e;} }*/ Public classSolution { PublicList<interval> Merge (list<interval>intervals) {List<Interval> result =NewArraylist<interval>(); intLen =intervals.size (); if(Len < 2 )            returnintervals; Comparator<Interval> Comparator =NewComparator<interval>(){             Public intCompare (Interval i1,interval i2) {if(i1.start>I2.start)return1; Else                     return-1;        }        };        Collections.sort (intervals, comparator); int[] start =New int[Len]; int[] End =New int[Len];  for(inti = 0;i<len;i++) {Start[i]=Intervals.get (i). Start; End[i]=Intervals.get (i). end; }        intBegin,over; inti = 0,j = 1;  while(i<Len) {            if(i = = len-1) {Result.add (Intervals.get (i)); returnresult; } Interval ans=NewInterval (); Ans.start=Start[i]; Begin=Start[j]; over=End[i];  while(Over >=begin) { over= Over>end[j]?Over:end[j]; J++; if(J = =len) Break; Begin=Start[j]; } ans.end=Over ;                        Result.add (ANS); I=J; J= J+1; }                        returnresult; }}

And then found that it is not time-out, but there is a problem with compare rewriting.

JDK 1.7 Before compare can only return 1 and-1, but 1.7 when two number is the same need to return 0, because compare (a, B) and compare (B,a) to return the opposite number, so there is a problem here, and then slightly modified, and made some minor adjustments, The results were satisfactory.

/*** Definition for a interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0; } * Interval (int s, int e) {start = s; end = e;} }*/ Public classSolution { PublicList<interval> Merge (list<interval>intervals) {List<Interval> result =NewArraylist<interval>(); intLen =intervals.size (); if(Len < 2 )            returnintervals; Comparator<Interval> Comparator =NewComparator<interval>(){             Public intCompare (Interval i1,interval i2) {if(i1.start>I2.start)return1; Else if(I1.start = =I2.start)return0; Else                    return-1;        }        };        Collections.sort (intervals, comparator); intBegin,over; inti = 0,j = 1,k = 0;  while(i< Len && J <Len) {Interval ans=NewInterval (); Ans.start=Intervals.get (i). Start; Begin=Intervals.get (j). Start; over=Intervals.get (i). end;  while(Over >=begin) { over= Over>intervals.get (j). End?Over:intervals.get (j). End; J++; if(J = =len) Break; Begin=Intervals.get (j). Start; } ans.end=Over ;            Result.add (ANS); I=J; J= J+1; K++; }        if(Result.get (k-1). End < Intervals.get (len-1). Start) Result.add (Intervals.get (Len-1)); returnresult; }}

And then find out the other person's answer, there is a hard to find the details, that is, if the start and end respectively into two arrays, and then sorted, the final result and directly to the intervals order to get the same results, and so, then directly reached the fastest. In fact, the idea is the same, or previously said, the algorithm is one hand, another aspect is the storage mode. Both are critical.

/*** Definition for a interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0; } * Interval (int s, int e) {start = s; end = e;} }*/ Public classSolution { PublicList<interval> Merge (list<interval>intervals) {List<Interval> result =NewArraylist<interval>(); intLen =intervals.size (); if(Len < 2 )            returnintervals; int[] start =New int[Len]; int[] End =New int[Len];  for(inti = 0;i<len;i++) {Start[i]=Intervals.get (i). Start; End[i]=Intervals.get (i). end;        } arrays.sort (start);        Arrays.sort (end); intBegin,over; inti = 0,j = 1;  while(i<Len) {            if(i = = len-1) {Result.add (NewInterval (Start[i],end[i])); returnresult; } Interval ans=NewInterval (); Ans.start=Start[i]; Begin=Start[j]; over=End[i];  while(Over >=begin) { over= Over>end[j]?Over:end[j]; J++; if(J = =len) Break; Begin=Start[j]; } ans.end=Over ;                        Result.add (ANS); I=J; J= J+1; }                        returnresult; }}

Leetcode the Merge intervals-----java

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.