Insert Interval, Leetcode 解题笔记

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

其实就是三种情况,一次遍历就解决了。

insert-interval--650x351
图片来自:http://www.programcreek.com/2012/12/leetcode-insert-interval/

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
        ArrayList<Interval> ret = new ArrayList<Interval>();
        boolean flag = false;
        if(intervals.size() == 0){
            ret.add(newInterval);
            return ret;
        }
        
        for(int i = 0; i < intervals.size(); i++){
            Interval cur = intervals.get(i);
            if(newInterval.start > cur.end){
                ret.add(cur);
            }
            else if(newInterval.end < cur.start){
                ret.add(newInterval);
                for(int j = i; j < intervals.size(); j++){
                    ret.add(intervals.get(j));
                }
                flag = true;
                break;
            }
            else{
                newInterval = new Interval(Math.min(newInterval.start, cur.start), Math.max(newInterval.end, cur.end));
            }
        }
        if(!flag)
            ret.add(newInterval);
        return ret;
    }
}

同样的思路,人家写的实在是太简洁了:

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
 
        ArrayList<Interval> result = new ArrayList<Interval>();
 
        for(Interval interval: intervals){
            if(interval.end < newInterval.start){
                result.add(interval);
            }else if(interval.start > newInterval.end){
                result.add(newInterval);
                newInterval = interval;        
            }else if(interval.end >= newInterval.start || interval.start <= newInterval.end){
                newInterval = new Interval(Math.min(interval.start, newInterval.start), Math.max(newInterval.end, interval.end));
            }
        }
 
        result.add(newInterval); 
 
        return result;
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s