My Simple JAVA Solution


  • 0
    X

    class Solution {

    public List<Interval> merge(List<Interval> intervals) {
        Collections.sort(intervals,new Comparator<Interval>(){
            public int compare(Interval i1,Interval i2){
                return i1.start-i2.start;
            }
        });
        for(int i=0;i<intervals.size()-1;i++){
            if(intervals.get(i).end>=intervals.get(i+1).start){
                if(intervals.get(i+1).end<=intervals.get(i).end){
                    intervals.remove(i+1);
                    i--;
                    continue;
                }
                Interval inter = new Interval(intervals.get(i).start,intervals.get(i+1).end);
                intervals.remove(i);
                intervals.set(i,inter);
                i--;
            }
        }
        return intervals;
    }
    

    }


Log in to reply
 

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