Java Clean Code (IN PLACE)


  • 0
    S
    public class Solution {
    public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
    	int index = 0;
    	while (index < intervals.size() && newInterval.start > intervals.get(index).end) {
    		index++;
    	}
    
    	for (int i = index; i < intervals.size(); ++i) {
    		Interval tmp = intervals.get(i);
    		if (tmp.start > newInterval.end) {
    			break;  
    		}
    		newInterval.start = Math.min(tmp.start,newInterval.start);
    		newInterval.end = Math.max(tmp.end,newInterval.end);
    		intervals.remove(i);
    		i--;
    	}
    
    	intervals.add(index,newInterval);
    	return intervals;
    }
    

    }


Log in to reply
 

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