156. Merge intervals "Lintcode by Java"

Source: Internet
Author: User

Description

Given a collection of intervals, merge all overlapping intervals.

Example

Given intervals = merged intervals:

[                     [  (1, 3),               (1, 6),  (2, 6),      =>       (8, 10),  (8, 10),              (15, 18)  (15, 18)            ]]
Challenge

O (n log n) Time and O (1) Extra space.

Test Instructions : Given a set, there are a number of unordered intervals, which require the merging of overlapping sections. The example of this topic is not very good, this example gives the impression that these intervals are orderly. Orderly and disorderly, in the same way, the results may not be the same, for example, I began to understand the order, the error is as follows:

Input
[(2,3), (4,5), (6,7), (8,9), (1,10)]
Output
[(2,3), (4,5), (6,7), (1,10)]
Expected
[(1,10)]
Hint
Review your code and make sure your algorithm are correct. Wrong answer usually caused by typos if your algorithm is correct.
Input test data (one parameter per line.)

Although it is unordered, we can manually sort it according to the size of the first value, using the Sort method in the collections class (check the API) to sort the list. After you have finished, you can merge the intervals within the collection. The merge method is similar to this problem: 30. Insert Interval "Lintcode by Java"

Apply a new set, and then use a loop to compare the ordered interval 22, and if no merging is required, the former is added to the new set, which continues to merge with the subsequent intervals. The code is as follows:

1  Public classSolution {2     /**3      * @paramintervals:interval list.4      * @return: A new interval list.5      */6      //determine if two intervals intersect7      PublicList<interval> Merge (list<interval>intervals) {8         //Write your code here9         if(Intervals.size () ==0| | Intervals.size () ==1)Ten            returnintervals; Onelist<interval>res=NewArraylist<interval>(); ACollections.sort (intervals,NewIntervalcompare ()); -Interval Last=intervals.get (0); -          for(intI=1;i<intervals.size (); i++){ theInterval cur=Intervals.get (i); -             if(last.end<Cur.start) { - Res.add (last); -last=cur; +}Else{ -last.start=math.min (last.start,cur.start); +Last.end=Math.max (last.end,cur.end); A             } at         } - Res.add (last); -         returnRes; -     } -       Private classIntervalcompareImplementsComparator<interval>{ -          Public intCompare (Interval a,interval b) { in             returna.start-B.start; -         } to     } +}

If there is any mistake, please criticize

156. Merge intervals "Lintcode by 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.