Java with No extra space


  • 1
    C
    public class Solution {
        public List<Interval> merge(List<Interval> intervals) {
            if(intervals.size() ==0)return intervals;
            Collections.sort(intervals, (v1,v2) -> v1.start - v2.start);
            for(int i = 1; i < intervals.size(); ){
                if(intervals.get(i).start <= intervals.get(i-1).end){
                    Interval change = new Interval(intervals.get(i-1).start, Math.max(intervals.get(i-1).end,intervals.get(i).end));
                    intervals.set(i-1, change);
                    intervals.remove(i);
                }else{
                    i++;
                }
            }
            return intervals;
        }
    }

Log in to reply
 

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