Demo entry 6714511

leetcode56

   

Submitted by anonymous on Feb 13, 2018 at 22:24
Language: Java. Code size: 796 Bytes.

class Solution {
    public List<Interval> merge(List<Interval> intervals) {
    if (intervals.size() <= 1)
        return intervals;
        //sorting
    intervals.sort((intvl1, intvl2) -> Integer.compare(intvl1.start, intvl2.start));
    
    List<Interval> answer = new LinkedList<Interval>();
    int start = intervals.get(0).start;
    int end = intervals.get(0).end;
    
    for (Interval interval : intervals) {
        if (interval.start <= end) 
            end = end>=interval.end?end:interval.end;
        else {             
            answer.add(new Interval(start, end));
            start = interval.start;
            end = interval.end;
        }
    }
    
    // Add the last interval
    answer.add(new Interval(start, end));
    return answer;
}
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).