Java 12 lines solution


  • 0
    S
    public class Solution {
        public List<Interval> merge(List<Interval> intervals) {
            Collections.sort(intervals, (a, b) -> a.start - b.start);
            List<Interval> r = new ArrayList<>();
            for(Interval curr : intervals){
                if(r.size() == 0 || curr.start > r.get(r.size() - 1).end)
                    r.add(new Interval(curr.start, curr.end));
                else
                    r.get(r.size() - 1).end = Math.max(r.get(r.size() - 1).end, curr.end);
            }
            return r;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.