Clear and Easy 12 Line Java Solution


  • 0
    M
    public List<Interval> insert(List<Interval> is, Interval ni) {
    	int i = 0;
    	int start = ni.start, end = ni.end;
    	while (i < is.size() && ni.start > is.get(i).end)
    		i++;
    
    	while (i < is.size() && ni.end >= is.get(i).start) {
    		start = Math.min(is.get(i).start, start);
    		end = Math.max(is.get(i).end, end);
    		is.remove(i);
    	}
    
    	is.add(i, new Interval(start, end));
    
    	return is;
    
    }

  • 0
    S

    maybe use binary search to find the i in the first while loop will be faster. But your codes are indeed clean and easy to understand. Good job. Thx


Log in to reply
 

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