# Insert Interval Array

208人阅读 评论(0)

Runtime Error

void quick_sort(vector<Interval> &array, int left, int right) {
if(left < right) {
int i = left, j = right;
Interval x = array[i];
while(i < j) {
while(i < j && array[j].start >= x.start) {
--j;
}
if(i < j) {
swap(array[i], array[j]);
++i;
}
while(i < j && array[i].start < x.start) {
++i;
}
if(i < j) {
swap(array[i], array[j]);
--j;
}
}
array[i] = x;
quick_sort(array, left, i - 1);
quick_sort(array, i + 1, right);
}
}
void clear(Interval &x) {
x.start = 1;
x.end = 0;
}
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
int n = intervals.size();
vector<Interval> ans;
for(int i = 0; i < n; ++i) {
if(newInterval.start > intervals[i].end || newInterval.end < intervals[i].start) {
continue;
}
if(newInterval.start == intervals[i].end) {
newInterval.start = intervals[i].start;
clear(intervals[i]);
}else if(newInterval.end == intervals[i].start) {
newInterval.end = intervals[i].end;
clear(intervals[i]);
}else {
newInterval.start = min(newInterval.start, intervals[i].start);
newInterval.end = max(newInterval.end, intervals[i].end);
clear(intervals[i]);
}
}
//delete
for(int i = 0; i < n; ++i) {
if(!(intervals[i].start == 1 && intervals[i].end == 0)) {
ans.push_back(intervals[i]);
}
}
ans.push_back(newInterval);
show(ans);
quick_sort(ans, 0, n - 1);
return ans;
}

intervals : [1,2],[2,3],[3,4],[4,5],....,[19999,20000]
newInterval : [0,20001]

/**
* 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) {
vector<Interval>::iterator it = intervals.begin();
while(it != intervals.end()) {
if(newInterval.end < it->start) {
intervals.insert(it, newInterval);
return intervals;
}else if(newInterval.start > it->end) {
it++;
continue;
}else {
newInterval.start = min(it->start, newInterval.start);
newInterval.end = max(it->end, newInterval.end);
it = intervals.erase(it);
}
}
intervals.insert(intervals.end(), newInterval);
return intervals;
}
};

/**
* 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) {
vector<Interval> res;
int n = intervals.size();
for(int i = 0; i < n; ++i) {
if(newInterval.end < intervals[i].start) {
res.push_back(newInterval);
res.insert(res.end(), intervals.begin() + i, intervals.end());
return res;
}else if(newInterval.start > intervals[i].end) {
res.push_back(intervals[i]);
}else {
newInterval.start = min(newInterval.start, intervals[i].start);
newInterval.end = max(newInterval.end, intervals[i].end);
}
}
res.push_back(newInterval);
return res;
}
};
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
Always Be Coding
Today, I work for myself. Building something new.
个人资料
• 访问：810736次
• 积分：10396
• 等级：
• 排名：第1600名
• 原创：445篇
• 转载：54篇
• 译文：0篇
• 评论：114条
文章分类