My java accepted solution


  • -2
    I
     public class Solution {
    	public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
    	    ArrayList<Interval> temp = new ArrayList<Interval>();
    	    ArrayList<Interval> result = new ArrayList<Interval>();
    	    result.addAll(intervals);
    	    for (Interval inter : intervals) {
    	        if (isOverlapped(inter, newInterval)) {
    	            result.remove(inter);
    	            temp.add(inter);
    	        }
    	    }
            int start = newInterval.start;
            int end = newInterval.end;
            for (Interval x : temp) {
                start = Math.min(start, x.start);
                end = Math.max(end, x.end);
            }
            Interval newInter = new Interval(start, end);
            int i = 0;
            while (true) {
                if (start < result.get(i).start || i == result.size()) break;
                  i++
            }
            result.add(i, newInter);
    		return result;
    	}
    	
    	private boolean isOverlapped (Interval first, Interval second) {
    	    if (first.start > second.end || first.end < second.start) {
    	        return false;
    	    }
    	    return true;
    	}
    }

  • 0
    Z

    Accepted? It has both compile and runtime errors...


  • 0
    I

    Sorry for a mistake in the while. Change "true" to "i < result.size()"
    while (i < result.size()) {
    if (start < result.get(i).start || i == result.size()) break;
    i++;
    }


Log in to reply
 

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