-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode-56-Merge_Intervals.cpp
68 lines (57 loc) · 1.67 KB
/
leetcode-56-Merge_Intervals.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
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
class Solution {
public:
std::vector<std::vector<int>> merge(std::vector<std::vector<int>>& intervals) {
std::vector<std::vector<int>> res;
std::sort(intervals.begin(), intervals.end(), [](const std::vector<int> & a, const std::vector<int> & b)
{
return a[0] < b[0];
});
int start_pos = intervals[0][0],
end_pos = intervals[0][1];
size_t vec_size = intervals.size();
for(size_t i = 0; i<vec_size; ++i)
{
if (((i<(vec_size-1)) && (end_pos<intervals[i+1][0])) ||
(i==(vec_size-1)))
{
res.emplace_back(std::vector<int>{start_pos, end_pos});
if (i!=(vec_size-1))
{
start_pos = intervals[i+1][0];
end_pos = intervals[i+1][1];
}
}
else
{
end_pos = std::max(end_pos, intervals[i+1][1]);
}
}
return res;
}
};
int main(){
std::vector<std::vector<int>> intervals = {{1,3},{2,6},{8,10},{15,18}};
auto res = Solution().merge(intervals);
std::cout << "[";
for(size_t r = 0; r<res.size(); ++r)
{
std::cout << "[";
for(size_t c = 0; c<res[r].size(); ++c)
{
std::cout << res[r][c];
if(c!=(res[r].size()-1)){
std::cout << ",";
}
}
std::cout << "]";
if(r!=(res.size()-1)){
std::cout << ",";
}
}
std::cout << "]" << std::endl;
return EXIT_SUCCESS;
}