One Day Together Leetcode series (i) Title
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].
(ii) Problem solving
/* Problem Solving steps: 1, the vector inside each interval structure according to the value of start in ascending order 2, traverse the entire intervals, if there is a repeating region on the merge, if not have to save to ret inside * */** * Definition for an interval. * struct INTERVAL {* int start; * int end; * Interval (): Start (0), end (0) {} * Interval (int s, int e): Start (s), End (e) {} *}; */classSolution { Public: vector<Interval>Merge vector<Interval>& intervals) { vector<Interval>Retif(Intervals.empty ())returnRet Sort (Intervals.begin (), Intervals.end (), Comparein);/// in ascending order of start valuesInterval tmp = intervals[0];//Initialize for(inti =1; I < intervals.size (); i++) {if(intervals[i].start<=tmp.end) tmp.end = max (tmp.end,intervals[i].end);//Update End Else{ret.push_back (TMP);//No repeat area to press into RETTMP = Intervals[i];//Update the value of TMP to continue traversing}} ret.push_back (TMP);returnRet }Static BOOLComparein (Constinterval& I1,Constinterval& I2)//Comparison function{returni1.start<i2.start; }};
"One Day together Leetcode" #56. Merge intervals