leetcode::Merge Intervals
/**
* 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 cmp( 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
int len = intervals.size();
if( len <= 1 )
{
return intervals;
}
vector<Interval> ret;
sort( intervals.begin(),intervals.end(), cmp );
Interval tmp = intervals[0];
for( int i = 1; i < len ; i++ )
{
if( tmp.end < intervals[i].start )
{
ret.push_back(tmp);
tmp = intervals[i];
}
if( tmp.end < intervals[i].end)
{
tmp.end = intervals[i].end;
}
}
ret.push_back(tmp);
return ret;
}
};