[LeetCode] 56. Merge Intervals(vector sort)

[LeetCode] 56. Merge Intervals(vector sort)

 /**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution
{
public:
static bool cmp(Interval &a,Interval &b)
{
return a.start < b.start;
}
vector<Interval> merge(vector<Interval>& intervals)
{
if(intervals.empty()) return vector<Interval> {};
sort(intervals.begin(),intervals.end(),cmp);
vector<Interval> res;
res.push_back(intervals[]);
for(int i = ; i < intervals.size(); i ++)
{
if(intervals[i].start > res.back().end)
res.push_back(intervals[i]);
else
res.back().end = max(res.back().end, intervals[i].end);
}
return res;
}
};
上一篇:C与C++相互调用


下一篇:leetcode 56. Merge Intervals 、57. Insert Interval