forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0057.cpp
27 lines (25 loc) · 830 Bytes
/
0057.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <vector>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval)
{
vector<Interval> res;
int i = 0;
while (i < intervals.size() and intervals[i].end < newInterval.start)
{
res.push_back(intervals[i++]);
}
while (i < intervals.size() and intervals[i].start <= newInterval.end)
{
newInterval.start = min(intervals[i].start, newInterval.start);
newInterval.end = max(intervals[i].end, newInterval.end);
++i;
}
res.push_back(newInterval);
for (; i < intervals.size(); ++i) res.push_back(intervals[i]);
return res;
}
};