-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode-57-Insert_Interval-V1.cpp
69 lines (60 loc) · 1.9 KB
/
leetcode-57-Insert_Interval-V1.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
class Solution {
public:
std::vector<std::vector<int>> insert(std::vector<std::vector<int>>& intervals, std::vector<int>& newInterval) {
if(intervals.empty())
{
intervals.emplace_back(newInterval);
}
for(size_t i = 0; i<intervals.size(); ++i)
{
if(newInterval[0]<=intervals[i][1] && newInterval[1]>=intervals[i][0]) {
intervals[i][1] = std::max(newInterval[1], intervals[i][1]);
intervals[i][0] = std::min(newInterval[0], intervals[i][0]);
} else if(newInterval[0]<intervals[i][0]) {
intervals.insert((intervals.begin()+i), newInterval);
break;
} else if(i==(intervals.size()-1)) {
intervals.emplace_back(newInterval);
break;
} else {
continue;
}
while(i<(intervals.size()-1) && intervals[i+1][0]<=intervals[i][1])
{
intervals[i][1] = std::max(intervals[i+1][1], intervals[i][1]);
intervals.erase((intervals.begin()+(i+1)));
}
break;
}
return intervals;
}
};
int main(){
std::vector<std::vector<int>> intervals = {{1,3},{6,9}};
std::vector<int> newInterval = {2,5};
auto res = Solution().insert(intervals, newInterval);
std::cout << "[";
for(size_t i = 0; i<res.size(); ++i)
{
std::cout << "[";
for(size_t j = 0; j<res[i].size(); ++j)
{
std::cout << res[i][j];
if(j<(res[i].size()-1))
{
std::cout << ",";
}
}
std::cout << "]";
if(i<(res.size()-1))
{
std::cout << ",";
}
}
std::cout << "]" << std::endl;
return EXIT_SUCCESS;
}