Merge Intervals
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ //方法:先sort,然後再考慮情況合並。復雜度:O(nlogn) class Solution { public: struct compare { bool operator()( const Interval& a , const Interval& b) const { if( a.start==b.start) return a.end < b.end; else return a.start < b.start; } }; vectormerge(vector &intervals) { if(intervals.size()==0) return vector (); sort(intervals.begin() , intervals.end() , compare()); vector ans; Interval temp; temp = intervals[0]; for(int i=1; i