Insert Interval

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].

注意更新的时候只更新newInterval,然后最后再插进去就行了。 因为顺序无所谓。

 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() { start = 0; end = 0; }
 7  *     Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 public class Solution {
11     public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
12         // Note: The Solution object is instantiated only once and is reused by each test case.
13         ArrayList<Interval> results = new ArrayList<Interval>();
14         boolean sig = false;
15         for(int i = 0; i < intervals.size(); i ++)
16         {
17             if(intervals.get(i).end < newInterval.start)
18             {
19                 results.add(intervals.get(i));    
20             }
21             else
22             {
23                 newInterval.start = Math.min(newInterval.start, intervals.get(i).start);
24             }
25             if(intervals.get(i).start > newInterval.end)
26             {
27                 if(!sig)
28                 {
29                     results.add(newInterval);
30                     sig = true;
31                 }
32                 results.add(intervals.get(i));
33             }
34             else
35             {
36                 newInterval.end = Math.max(newInterval.end, intervals.get(i).end);
37             }
38         }
39         if(!sig)
40         {
41             results.add(newInterval);
42         }
43         return results;
44     }
45 }

第二遍:

不使用额外的存储空间:

 1 public class Solution {
 2     public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
 3         // Note: The Solution object is instantiated only once and is reused by each test case.
 4         if(intervals == null || intervals.size() == 0){
 5             ArrayList<Interval> result = new ArrayList<Interval>(newInterval);
 6             result.add(newInterval); 
 7             return result;
 8         }
 9         for(int i = 0; i < intervals.size(); i ++){
10             if(i == 0)
11         }
12         intervals.get(0).start = intervals.get(0).start > newInterval.start ?  newInterval.start : intervals.get(0).start;
13         pq.add(newInterval);
14         result.add(pq.poll());//poll out the minimum element in the queue
15         int max_end = result.get(0).end;
16         while(pq.size() != 0){
17             Interval tmp = pq.poll();
18             if(tmp.start <= max_end){
19                 max_end = max_end > tmp.end ? max_end : tmp.end;
20                 result.get(result.size() - 1).end = max_end;
21             }else{
22                 result.add(tmp);
23                 max_end = tmp.end;
24             }
25         }
26         return result;
27     }
28 }

 

 

posted on 2013-10-04 07:35  Step-BY-Step  阅读(232)  评论(0编辑  收藏  举报

导航