. Merge Intervals-leetcode-java

Source: Internet
Author: User

"The original in the SAE's blog, all transferred to the CSDN. "
. Merge Intervals-leetcode-javaPosted in2016/02/08

Test instructions

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

Given some list sets defined as interval interval classes, merge overlapping intervals.

Idea: First judge the whole intervals set is not null or the length of 0 or 1, if it is, directly return to intervals;

Otherwise use Collections.sort to the whole intervals set to use a certain sort of rules, this rule is: according to each interval start from small to large sort.

Then define a result set LS, traverse the entire intervals collection from beginning to end, if there are intervals coincident, merge into merged, then assign to begin, add to LS, otherwise directly add two intervals to the result set.

The collation here is to redefine the intervalcomparator implementation of the comparator interface, and then rewrite the Compare method, here is the return I1.start-i2.start, which is the ascending sort.

The total feeling of return to this sentence is not thoroughly understood why this is ascending. First of all, remember, a little understand.

/*** Definition for an interval.* public class Interval {* int start;* int end;* Interval () {start = 0; end = 0;}* Interval (int s, int e) {start = s; end = e;} * } */Public class Solution {Public list<interval> Merge (list<interval> intervals) {if (intervals==null | | intervals.size () <=1) return intervals; Collections.sort (intervals,new intervalcomparator ()); arraylist<interval> ls=new arraylist<interval> ();Interval begin=intervals.get (0);for (int i=1;i<intervals.size (); i++) {Interval Curr = Intervals.get (i);if (begin.end>=curr.start) {Interval merged=new Interval (Begin.start,math.max (begin.end,curr.end));//Ls.add (merged);//curr=merged; begin=merged; }else{Ls.add (begin);Begin=curr;            }        }Ls.add (begin);return LS;    }class Intervalcomparator implements Comparator<interval>{public int Compare (Interval i1,interval i2) {//Return I1.start.compareTo (I2.start); return i1.start-i2.start;         }    }}  After referring to many times Leetcode, found this site http://www.programcreek.com is a pretty good website, a lot of blog is reference to the above code. This place used to be aboutCollections.sort (list, new Xxxcomparator ())With regard to the usage of collections.sort, this article is very good: http://blog.csdn.net/tjcyjd/article/details/6804690 also involves thinking about comparator and comparable, the difference between the two interfaces, the following article is very easy to understand http://www.cnblogs.com/sunflower627/p/3158042.html Posted in leetcode|  tags are java, Leetcode| Post a reply

. Merge Intervals-leetcode-java

Related Article

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.