-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMerge Intervals.cpp
More file actions
23 lines (22 loc) · 845 Bytes
/
Copy pathMerge Intervals.cpp
File metadata and controls
23 lines (22 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
std::vector<std::vector<int>> merge(std::vector<std::vector<int>>& intervals) {
std::sort(intervals.begin(), intervals.end(), [](std::vector<int>& vec1, std::vector<int>& vec2) ->bool { return vec1[0] < vec2[0]; });
int j = 1;
std::vector<std::vector<int>> answer;
int temp[2] = {intervals[0][0],intervals[0][1]};
while (j!=intervals.size()) {
if (temp[1] >= intervals[j][0] && temp[1]<=intervals[j][1])
temp[1] = intervals[j][1];
else if (temp[1] < intervals[j][0] ) {
std::vector <int> ans{ temp[0],temp[1] };
answer.emplace_back(std::move(ans));
temp[0] = intervals[j][0];
temp[1] = intervals[j][1];
}
j++;
}
answer.push_back(std::vector<int>{temp[0], temp[1]});
return answer;
}
};