标签:
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]
.
Array Sort
#include <vector> #include <iostream> using namespace std; /** * Definition for an interval. */ struct Interval { int start; int end; Interval() : start(0), end(0) {} Interval(int s, int e) : start(s), end(e) {} }; class Solution { public: vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) { int n = intervals.size(); if(n<1) return vector<Interval>{{newInterval.start,newInterval.end}}; vector<Interval> ret; // if(n==1){ // if(newInterval.end<intervals[0].start){ // ret.push_back(newInterval); ret.push_back(intervals[0]); // return ret; // } // if(intervals[0].end<newInterval.start){ // ret.push_back(intervals[0]); ret.push_back(newInterval); // return ret; // } // } int idx1 =binsearch(intervals,newInterval.start); int idx2 =binsearch(intervals,newInterval.end); int idx_tmp,newStart; if(intervals[idx1].end>=newInterval.start){ idx_tmp = idx1-1; newStart = intervals[idx1].start<newInterval.start?intervals[idx1].start:newInterval.start; } else{ idx_tmp=idx1; newStart = newInterval.start; } for(int i=0;i<=idx_tmp;i++) ret.push_back(intervals[i]); int newEnd; if(newInterval.end<intervals[idx2].start){ newEnd = newInterval.end; idx_tmp = idx2; } else{ newEnd = intervals[idx2].end>newInterval.end?intervals[idx2].end:newInterval.end; idx_tmp = idx2+1; } ret.push_back(Interval(newStart,newEnd)); for(int i=idx_tmp;i<n;i++) ret.push_back(intervals[i]); return ret; } int binsearch(vector<Interval> &ints,int target) { if(ints.size()==1) return 0; int lft = 0,rgt = ints.size()-1; if(ints[rgt].start<=target) return rgt; if(ints[lft].start>=target) return lft; int ret = 0; do{ int mid = (lft+rgt)/2; if(ints[mid].start>target) rgt=mid; else lft=mid; ret = lft; if(ints[ret+1].start>target) break; }while(lft+1<rgt); return ret; } }; int main(){ vector<Interval> intervals{{1,2},{3,5},{6,7},{8,10},{12,16}}; // vector<Interval> intervals{{1,2}}; // cout<<intervals[0].start<<" "<<intervals[1].end<<endl; Interval newInterval(-1,0); Solution sol; vector<Interval> ret = sol.insert(intervals,newInterval); for(int i=0;i<ret.size();i++) cout<<ret[i].start<<" "<<ret[i].end<<endl; return 0; }
[LeetCode] Insert Interval 二分搜索
标签:
原文地址:http://www.cnblogs.com/Azhu/p/4202574.html