Demo entry 6787362

aa

   

Submitted by anonymous on Apr 12, 2019 at 03:17
Language: Java. Code size: 1.1 kB.

/** 
 * Definition for an interval. 
 * public class Interval { 
 *     int start; 
 *     int end; 
 *     Interval() { start = 0; end = 0; } 
 *     Interval(int s, int e) { start = s; end = e; } 
 * } 
 */  
class Solution {  
    public List<Interval> merge(List<Interval> intervals) {  
        List<Interval> res = new ArrayList<Interval>();  
        if(intervals.size() < 2) {  
            return intervals;  
        }  
        Collections.sort(intervals, new Comparator<Interval>() {  
        @Override  
            public int compare(Interval o1, Interval o2) {  
                return o1.start - o2.start;  
            }  
        });  
  
        Interval curr = intervals.get(0);  
        for(int i = 1; i < intervals.size(); i++) {  
            if(curr.end >= intervals.get(i).start) {  
                curr.end = Math.max(curr.end, intervals.get(i).end);  
            }else {  
                res.add(curr);  
                curr = intervals.get(i);  
            }  
        }  
        res.add(curr);  
        return res;  
    }  
}  

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).