Java solution of 95%


  • 0
    W
            int i = 0;
    	int n = intervals.size();
    	List<Interval> result = new ArrayList<>();
    	// find the start of overlap
    	while(i < n && newInterval.start > intervals.get(i).end) result.add(intervals.get(i++));
    	// the start of the merge part
    	if(i < n && newInterval.start > intervals.get(i).start) {
    	    newInterval.start = intervals.get(i).start;
    	}
    
    	// find the end place of the overlap
    	while(i < n && newInterval.end >= intervals.get(i).start) i++;
    	// the end of the merge part
    	if(i > 0 && i <= n && newInterval.end < intervals.get(i-1).end) {
    		newInterval.end = intervals.get(i - 1).end;
    	}
    	result.add(newInterval);
    
    	while(i < n) result.add(intervals.get(i++));
    
    	return result;

Log in to reply
 

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