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].
1 public struct Interval 2 { 3 public int start; 4 public int end; 5 6 public Interval(int s, int e) 7 { 8 start = s; 9 end = e; 10 } 11 } 12 13 public static List<Interval> InsertInterval(List<Interval> intervals, Interval newInterval) 14 { 15 int newS = -1; 16 int newE = -1; 17 List<Interval> ret = new List<Interval>(); 18 19 if (intervals.Count == 0) 20 { 21 ret.Add(newInterval); 22 return ret; 23 } 24 25 26 if (newInterval.end < intervals[0].start) 27 { 28 ret.Add(newInterval); 29 foreach (var item in intervals) 30 { 31 ret.Add(item); 32 } 33 return ret; 34 } 35 36 if (newInterval.start > intervals[intervals.Count - 1].end) 37 { 38 foreach (var item in intervals) 39 { 40 ret.Add(item); 41 } 42 ret.Add(newInterval); 43 return ret; 44 } 45 46 if (newInterval.start <= intervals[0].start && newInterval.end >= intervals[intervals.Count - 1].end) 47 { 48 ret.Add(newInterval); 49 return ret; 50 } 51 52 if (newInterval.start <= intervals[0].start) 53 { 54 newS = newInterval.start; 55 foreach (var item in intervals) 56 { 57 if (newInterval.end > item.end) 58 { 59 continue; 60 } 61 if (newE == -1 && newInterval.end >= item.start && newInterval.end <= item.end) 62 { 63 newE = item.end; 64 ret.Add(new Interval(newS, newE)); 65 continue; 66 } 67 if (newE == -1 && newInterval.end < item.start) 68 { 69 newE = newInterval.end; 70 ret.Add(new Interval(newS, newE)); 71 ret.Add(item); 72 continue; 73 } 74 ret.Add(item); 75 } 76 return ret; 77 } 78 79 if (newInterval.end >= intervals[intervals.Count - 1].end) 80 { 81 newE = newInterval.end; 82 foreach (var item in intervals) 83 { 84 if (newInterval.start > item.end) 85 { 86 ret.Add(item); 87 continue; 88 } 89 if (newS == -1 && newInterval.start >= item.start && newInterval.start <= item.end) 90 { 91 newS = item.start; 92 ret.Add(new Interval(newS, newE)); 93 return ret; 94 } 95 if (newS == -1 && newInterval.start < item.start) 96 { 97 newS = newInterval.start; 98 ret.Add(new Interval(newS, newE)); 99 return ret; 100 } 101 } 102 return ret; 103 } 104 105 foreach (var item in intervals) 106 { 107 if (newS == -1 && newInterval.start < item.start) 108 { 109 newS = newInterval.start; 110 continue; 111 } 112 if (newS == -1 && newInterval.start >= item.start && newInterval.start <= item.end) 113 { 114 newS = item.start; 115 continue; 116 } 117 if (newS == -1 && newInterval.start > item.end) 118 { 119 ret.Add(item); 120 } 121 122 if (newInterval.end > item.end) 123 { 124 continue; 125 } 126 if (newE == -1 && newInterval.end >= item.start && newInterval.end <= item.end) 127 { 128 newE = item.end; 129 ret.Add(new Interval(newS, newE)); 130 continue; 131 } 132 if (newE == -1 && newInterval.end < item.start) 133 { 134 newE = newInterval.end; 135 ret.Add(new Interval(newS, newE)); 136 ret.Add(item); 137 continue; 138 } 139 140 ret.Add(item); 141 } 142 143 return ret; 144 }
代码分析:
分6种情况:
1. [0,1] [2,3] [5,6] [8,9]
2. [2,3] [5,6] [8,9] [10,11]
3. [1, 10]
[2,3] [5,6] [8,9]
4. [1, 4]
[2,3] [5,6] [8,9]
5. [7, 10]
[2,3] [5,6] [8,9]
6. [4, 7]
[2,3] [5,6] [8,9]