Beats 92% (Simple Java Solution)


  • 0
    E
    public List<Interval> merge(List<Interval> intervals) {
            List<Interval> res = new ArrayList<Interval>();
            if(intervals.size() == 0 || intervals == null) return res;
            res.add(intervals.get(0));
            for(int i=1; i<intervals.size(); i++){
                Interval y = intervals.get(i);
                for(int j=0; j<res.size(); j++){
                    Interval x = res.get(j);
                    if(y.start > x.end || y.end < x.start) continue;
                    else{
                        y.start = Math.min(x.start, y.start);
                        y.end = Math.max(x.end, y.end);
                        res.remove(j--);
                    }
                }
                res.add(y);
            }
            return res;
        }
    

Log in to reply
 

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