/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool isLess(const Interval& a, const Interval& b){
return a.start<b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<Interval> result;
result.clear();
if(intervals.size()==0)
return result;
sort(intervals.begin(),intervals.end(),isLess);
Interval current=intervals[0];
Interval next;
for(int i=1;i<intervals.size();++i){
next=intervals[i];
if(current.end<next.start){
result.push_back(current);
current=next;
} else {
current.end=current.end>next.end?current.end:next.end;
}
}
result.push_back(current);
return result;
}
};
Leetcode: Merge Intervals
最新推荐文章于 2021-11-16 17:15:28 发布