My easy JAVA solution :)


  • 0
    M
    public List<Interval> merge(List<Interval> intervals) {
            List<Interval> res = new ArrayList<>();
            if (intervals == null || intervals.size() == 0) {
                return res;
            }
            Collections.sort(intervals, new Comparator<Interval>(){
                public int compare(Interval a, Interval b) {
                    return a.start - b.start;
                }
            });
            int preEnd = Integer.MIN_VALUE;
            for (int i = 0; i < intervals.size(); i++) {
                Interval curr = intervals.get(i);
                if (curr.start > preEnd) {
                    res.add(curr);
                } else {
                    res.get(res.size() - 1).end = Math.max(res.get(res.size() - 1).end, curr.end);
                }
                preEnd = res.get(res.size() - 1).end;
            }
            return res;
        }
    

Log in to reply
 

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