-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode第56题.cpp
More file actions
35 lines (35 loc) · 947 Bytes
/
Leetcode第56题.cpp
File metadata and controls
35 lines (35 loc) · 947 Bytes
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
class Solution {
private:
static bool cmp(vector<int> &a,vector<int> &b)
{
return a[0]<b[0];
}
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
if(intervals.size()==1){
return intervals;
}
sort(intervals.begin(),intervals.end(),cmp);
vector<vector<int>> ans;
int begin=intervals[0][0],end=intervals[0][1];
for(int i=1;i<intervals.size();++i)
{
if(intervals[i][0]<=end){
end=max(end,intervals[i][1]);
}
else{
ans.push_back({begin,end});
begin=intervals[i][0];
end=intervals[i][1];
}
}
if(intervals.back()[0]<=end){
end=max(intervals.back()[1],end);
ans.push_back({begin,end});
}
else{
ans.push_back(intervals.back());
}
return ans;
}
};