Java , short and easy, fast.


  • 2
    Z
    public List<Interval> insert(List<Interval> ins, Interval in) {
        List<Interval> ret = new ArrayList<>();
    
        for (Interval n : ins) {
            /* 2 cases for non-overlap + 1 case for overlap */
            if (n.end < in.start)
                ret.add(n);
            else if (in.end < n.start) {
                ret.add(in);
                in = n;
            } else {// merge
                in = new Interval(Math.min(n.start, in.start), Math.max(n.end, in.end));
            }
        }
    
        ret.add(in);
        return ret;
    }

Log in to reply
 

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