vector<Interval> merge(vector<Interval> &intervals) {
     sort(intervals.begin(),intervals.end(),compInterval());
     vector<Interval> ret;
     for(int i=0; i<intervals.size(); i++) {
         if(ret.empty() || ret.back().end < intervals[i].start)  // no overlap
             ret.push_back(intervals[i]);
         else   // overlap
             ret.back().end = max(ret.back().end, intervals[i].end);
     }
     return ret;
 }
Exemple #2
0
 vector<Interval> merge(vector<Interval>& intervals) 
 {
     vector<Interval> retVec;
     if (intervals.empty())
     {
         return retVec;
     }
     sort(intervals.begin(), intervals.end(), compInterval());
     
     const int intervalSize = intervals.size();
     for (int count = 0; count < intervalSize; count++)
     {
         if (retVec.empty() || retVec.back().end < intervals[count].start)
         {
             retVec.push_back(intervals[count]);
         }
         else
         {
             retVec.back().end = max(retVec.back().end, intervals[count].end);
         }
     }
     return retVec;
 }