Short java code


  • 39
    T
    public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
        List<Interval> result = new ArrayList<Interval>();
        for (Interval i : intervals) {
            if (newInterval == null || i.end < newInterval.start)
                result.add(i);
            else if (i.start > newInterval.end) {
                result.add(newInterval);
                result.add(i);
                newInterval = null;
            } else {
                newInterval.start = Math.min(newInterval.start, i.start);
                newInterval.end = Math.max(newInterval.end, i.end);
            }
        }
        if (newInterval != null)
            result.add(newInterval);
        return result;
    }

  • 0
    L

    your solution is quite brilliant !


  • 0

    Simply nice solution!


  • 1
    W

    very brilliant!!!!!!!

    set the newInterval == null after it was inserted, which improved the following code running time.

        public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
            List<Interval> ret = new ArrayList<>();
            for(Interval i : intervals) {
                if(i.end >= newInterval.start && i.start <= newInterval.end) {
                    newInterval.start = Math.min(i.start, newInterval.start);
                    newInterval.end = Math.max(i.end, newInterval.end);
                } else {
                    ret.add(i);
                }
            }
    
            ret.add(newInterval);
            Collections.sort(ret, new Comparator<Interval>() {
                public int compare(Interval a, Interval b) {
                    return a.start - b.start;
                }
            });
            
            return ret;
        }

  • 0
    N

    Like your code. And my submit is similar

    class Solution {
        public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
            List<Interval> res = new ArrayList<Interval>();
            
            if (intervals == null || intervals.size() == 0) {
                res.add(newInterval);
                return res;
            }
            
            int start = newInterval.start;
            int end = newInterval.end;
            boolean added = false;
            
            for (Interval i : intervals) {
                
                // i.end before the start, always add to res first
                if (i.end < start) {
                    res.add(i);
                }
                
                // i.start after the end, need to add the "newInterval" first, then add i
                else if (i.start > end) {
                    if (!added) {
                        Interval interval = new Interval(start, end);
                        res.add(interval);
                        added = true;
                    }
                    res.add(i);
                }
                
                
                else {
                    // at here, if (i.start < start), that is (i.start < start && i.end >= start) due to the else{}
                    if (i.start < start) {
                        start = i.start;
                    }
                    
                    // at here, if i.end > end, that is (i.end > end && i.start <= end) due to the else{}
                    if (i.end > end) {
                        end = i.end;
                    }
                }
            }
            
            // make sure the interval is added.
            if (!added) {
                Interval interval = new Interval(start, end);
                res.add(interval);
                added = true;
            }
            return res;
        }
    }

  • 0
    W

    @ningzhao
    your's are not neat.


  • 0
    N

    @wzrthhj thanks for reply. Yep, my code is not as short. And I created two variables start & end, which were not necessary. Do you have any specific suggestion on the code that I can improve my code?


Log in to reply
 

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