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和erase函数代价有点高,会移动修改,故,不做原地的,空间换时间,直接新建一个好了。
法一:从前向后遍历,依次比较进行插入【leetcode超时,被嫌弃了!】
1 #include "stdafx.h" 2 #include <vector> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 struct Interval 8 { 9 int start; 10 int end; 11 Interval():start(0),end(0) {} 12 Interval(int s, int e):start(s),end(e) {} 13 }; 14 class Solution 15 { 16 public: 17 vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) 18 {//参考:书 19 //从前向后比较,看是否插入 20 auto it = intervals.begin(); 21 while(it != intervals.end()) 22 { 23 if(newInterval.end < it->start) 24 {//当前区间在待插入区间之前,直接插入待插入区间 25 intervals.insert(it, newInterval); 26 return intervals; 27 } 28 else if(newInterval.start > it->end) 29 {//当前区间大于待插入区间,跳过,继续判断 30 it++; 31 continue; 32 } 33 else 34 {//当前区间与待插入区间之间有重合部分 35 newInterval.start = min(newInterval.start,it->start); 36 newInterval.end = max(newInterval.end,it->end); 37 it = intervals.erase(it);//返回删除位置的下一个位置 38 } 39 } 40 intervals.insert(intervals.end(),newInterval);//防止待插入区间在最后 41 return intervals; 42 } 43 }; 44 int main() 45 { 46 Solution sol; 47 48 Interval data1[] = {Interval(1,3),Interval(6,9)}; 49 vector<Interval> test1(data1,data1+2); 50 //test1:Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9]. 51 for(auto &i : test1) 52 cout << "["<<i.start << ","<< i.end<<"]"; 53 cout << endl; 54 sol.insert(test1,Interval(2,5)); 55 for(auto &i : test1) 56 cout << "["<<i.start << ","<< i.end<<"]"; 57 cout << endl; 58 cout << endl; 59 60 Interval data2[] = {Interval(1,2),Interval(3,5),Interval(6,7),Interval(8,10),Interval(12,16)}; 61 vector<Interval> test2(data2,data2+5); 62 //test2:Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16]. 63 for(auto &i : test2) 64 cout << "["<<i.start << ","<< i.end<<"]"; 65 cout << endl; 66 sol.insert(test2,Interval(4,9)); 67 for(auto &i : test2) 68 cout << "["<<i.start << ","<< i.end<<"]"; 69 cout << endl; 70 71 return 0; 72 }
法二:空间换时间,不做原地操作,将结果存到新的vector中
1 class Solution 2 { 3 public: 4 vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) 5 {//参考:书 6 //从前向后比较,看是否插入 7 //改进之处:方法没变,但由于insert和erase函数代价有点高,会移动修改,故,不做原地的,空间换时间,直接新建一个好了。 8 vector<Interval> res; 9 int count = intervals.size(); 10 if(count == 0) 11 { 12 res.push_back(newInterval);//防止待插入区间在最后 13 return res; 14 } 15 16 int index = 0; 17 while(index<count) 18 { 19 if(newInterval.end < intervals[index].start) 20 {//当前区间在待插入区间之前,直接插入待插入区间 21 res.push_back(newInterval); 22 while(index<count) 23 { 24 res.push_back(intervals[index]);//剩余元素插入res 25 index++; 26 } 27 return res; 28 } 29 else if(newInterval.start > intervals[index].end) 30 {//当前区间大于待插入区间,跳过,继续判断 31 res.push_back(intervals[index]); 32 } 33 else 34 {//当前区间与待插入区间之间有重合部分 35 newInterval.start = min(newInterval.start,intervals[index].start); 36 newInterval.end = max(newInterval.end,intervals[index].end); 37 } 38 index++; 39 } 40 res.push_back(newInterval);//防止待插入区间在最后 41 return res; 42 } 43 };
参考:http://www.cnblogs.com/ganganloveu/p/4158450.html
时间: 2024-12-15 12:23:52