A simple java solution


  • 0
    M
    public List<Interval> merge(List<Interval> intervals) {
    		List<Interval> result = new ArrayList<>();
    		intervals.sort(new Comparator<Interval>() {
    			public int compare(Interval o1, Interval o2) {
    				return o1.start - o2.start;
    			}
    		});
    		while (!intervals.isEmpty()) {
    			Interval pre = intervals.remove(0);
    			while (!intervals.isEmpty()) {
    				if (intervals.get(0).start > pre.end)
    					break;
    				pre.end = Math.max(intervals.remove(0).end, pre.end);
    			}
    			result.add(pre);
    		}
    		return result;
    	}
    

Log in to reply
 

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