[Leetcode] 56. Merge intervals Java

Source: Internet
Author: User

Topic:

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

test instructions and analysis: give a number of intervals, merge overlapping intervals, and output the final result. Our analysis can be learned that for two intervals interval1 and Interval2, if Interval1.start<=interval2.start, there are several situations:

(1) Interval1.end<interval2.start, two intervals are not overlapping, then two intervals are added to the result set

(2) Interval1.end>=interval2.start, overlapping, if a, interval1.end>= Interval2.end, that Interval2 is Interval1 's sub-range, will interval1 join, and then skip Interval2;b, interval1.end< Interval2.end, merge two intervals and add the new interval to the result.

So we can set the given interval in ascending order of Strat, and for start the same, according to the end ascending order, in accordance with the above method of traversing interval sets can be obtained the final result:

Code:

/** * Definition for an interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0;} * Interval (int s, I NT e) {start = s; end = e;} *} */public class Solution {public list<interval> merge (list<interval> inte Rvals) {Collections.sort (intervals, new comparator<interval> () {//in ascending order of start and end @overridepublic int compare (Interval O1, Interval O2)        {//TODO auto-generated method stubif (O1.start!=o2.start) return o1.start-o2.start;else return o1.end-o2.end;}}); int n=intervals.size (); List<interval> reslist=new arraylist<> (); for (int i=0;i<n;i++) {if (Reslist.size () ==0) ResList.add ( Intervals.get (i));//First time add Else{interval temp= intervals.get (i); if (temp.start>temp.end) continue;else{if ( Reslist.get (Reslist.size ()-1). End>=temp.start) {///The last interval and the current interval are compared if (Reslist.get (Reslist.size ()-1) .end< Temp.end) {///The last interval does not contain the current interval then merge or continue traversing Interval NewInterval = new Interval (Reslist.get (reslist).Size ()-1). Start,temp.end);//merge overlapping interval reslist.remove (Reslist.size ()-1);//Delete overlapping bands and add merged Reslist.add between zones (NewInterval) ;}} else {reslist.add (temp);}}}}    return reslist; }}

  

[Leetcode] 56. 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.