# leetcode 57:Insert Interval

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].

class Solution {
public:
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
int size = intervals.size();
vector<Interval> result;
if (size == 0)
{
result.push_back(newInterval);
return result;
}
int i = 0,low,high;
while (i<size && intervals[i].end < newInterval.start)
{
result.push_back(intervals[i]);
++i;
}
if (i<size && newInterval.end < intervals[i].start)
result.push_back(newInterval);
else
{
low = i < size ? min(intervals[i].start, newInterval.start) : newInterval.start;
while (i < size && newInterval.end >= intervals[i].start) ++i;
high = i>0 ? max(intervals[i-1].end, newInterval.end) : newInterval.end;
Interval temp(low, high);
result.push_back(temp);
}
while (i < size)
{
result.push_back(intervals[i]);
++i;
}
return result;
}
};

class Solution {
public:
static bool comp(const Interval a, const Interval b)
{
return a.start < b.start ? 1 : 0;
}
vector<Interval> insert(vector<Interval>& intervals,Interval newInterval) {
intervals.push_back(newInterval);
vector<Interval> result;
int size = intervals.size();
if (size < 2) return intervals;
sort(intervals.begin(), intervals.end(), comp);
for (int i = 0; i<size; ++i)
{
Interval temp(intervals[i].start, intervals[i].end);
int high = intervals[i].end;
while (i < size - 1 && high >= intervals[i + 1].start)
{
if (intervals[i + 1].end > high) high = intervals[i + 1].end;
++i;
}
temp.end = high;
result.push_back(temp);
}
return result;
}
};

• 本文已收录于以下专栏：

## LeetCode --- 57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...
• makuiyu
• 2015年03月14日 23:10
• 1316

## [Java]LeetCode57 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...
• fumier
• 2015年08月18日 20:52
• 854

## leetCode 57.Insert Interval (插入区间) 解题思路和方法

Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the interva...
• xygy8860
• 2015年07月15日 14:11
• 740

## LeetCode 57 --- Insert Interval

• baidu_33893880
• 2017年02月19日 20:18
• 82

## LeetCode 57 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...
• niuooniuoo
• 2016年11月01日 15:59
• 72

## LeetCode（57） Insert Interval

• fly_yr
• 2015年09月14日 19:24
• 1011

## LeetCode #57: Insert Interval

Problem Statement(Source) Given a set of non-overlapping intervals, insert a new interval into the i...
• junchen1992
• 2016年09月17日 22:01
• 163

## leetcode || 57、Insert Interval

problem: Given a set of non-overlapping intervals, insert a new interval into the inter...
• hustyangju
• 2015年04月01日 16:06
• 634

## LeetCode #57 | Insert Interval

• lycronaldo
• 2017年04月24日 18:21
• 89

## LeetCode 57 - Insert Interval

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the interv...
• ywcpig
• 2016年03月09日 18:22
• 206

举报原因： 您举报文章：leetcode 57:Insert Interval 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)