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、先用两次二分查找找到和待插入的区间有关系的区间范围;
2、将有关系的区间做处理;
3、生成新的合并后的区间并返回;
解题步骤:
代码:
1 /** 2 * Definition for an interval. 3 * struct 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 class Solution { 11 public: 12 static bool partial_order(const Interval &a, const Interval &b) { 13 return a.end < b.start; 14 }; 15 16 vector<Interval> insert(std::vector<Interval> &intervals, Interval newInterval) { 17 vector<Interval>::iterator less = lower_bound(intervals.begin(), intervals.end(), newInterval, partial_order); 18 vector<Interval>::iterator greater = upper_bound(intervals.begin(), intervals.end(), newInterval, partial_order); 19 20 vector<Interval> answer; 21 answer.insert(answer.end(), intervals.begin(), less); 22 if (less < greater) { 23 newInterval.start = min(newInterval.start, (*less).start); 24 newInterval.end = max(newInterval.end, (*(greater - 1)).end); 25 } 26 answer.push_back(newInterval); 27 answer.insert(answer.end(), greater, intervals.end()); 28 29 return answer; 30 } 31 };
不用lower_bound和upper_bound写的AC烂代码,留作改进:
1 /** 2 * Definition for an interval. 3 * struct 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 class Solution { 11 public: 12 vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) { 13 if (intervals.empty()) 14 return vector<Interval> (1, newInterval); 15 int left = 0; 16 int right = intervals.size() - 1; 17 int size = intervals.size(); 18 int ins_start, ins_end; 19 20 while (left < right) { 21 int mid = (left + right) / 2; 22 if (intervals[mid].end < newInterval.start) 23 left = mid + 1; 24 else 25 right = mid; 26 } 27 ins_start = left; 28 29 left = left == 0 ? 0 : left - 1; 30 right = intervals.size() - 1; 31 while (left < right) { 32 int mid = (left + right) / 2 + 1; 33 if (newInterval.end < intervals[mid].start) 34 right = mid - 1; 35 else 36 left = mid; 37 } 38 ins_end = right; 39 40 if (ins_end == 0 && newInterval.end < intervals[0].start) { 41 intervals.insert(intervals.begin(), newInterval); 42 return intervals; 43 } 44 if (ins_start == size - 1 && newInterval.start > intervals[size - 1].end) { 45 intervals.push_back(newInterval); 46 return intervals; 47 } 48 49 vector<Interval> answer; 50 answer.insert(answer.end(), intervals.begin(), intervals.begin() + ins_start); 51 if (ins_start <= ins_end) { 52 newInterval.start = min(newInterval.start, intervals[ins_start].start); 53 newInterval.end = max(newInterval.end, intervals[ins_end].end); 54 } 55 answer.push_back(newInterval); 56 answer.insert(answer.end(), intervals.begin() + ins_end + 1, intervals.end()); 57 return answer; 58 59 } 60 };
时间: 2024-10-26 09:38:43