标签:
class Solution { public: static bool cmp(Interval &a,Interval &b) { return a.start<b.start; } vector<Interval> merge(vector<Interval>& intervals) { vector<Interval> res; if(intervals.size()==0)return res; sort(intervals.begin(),intervals.end(),cmp); int i=1; int first=0; int j=first; Interval tmpin; while(i<intervals.size()){ //第一种情况比如:[1,2] [3,4] if(intervals[i].start>intervals[j].end){tmpin.start=intervals[first].start;tmpin.end=intervals[j].end;res.push_back(tmpin); first=i; j=first; i=i+1; } //第二种 比如: [2,6] [3,7] else if(intervals[i].start<=intervals[j].end&&intervals[i].end>intervals[j].end) { i++; j=i-1; } //第三种 比如: [2,6] [1,3] else { i++; } } if(intervals[j].end>=intervals[i-1].end) {tmpin.start=intervals[first].start;tmpin.end=intervals[j].end;res.push_back(tmpin);} else{tmpin.start=intervals[first].start;tmpin.end=intervals[i-1].end;res.push_back(tmpin);} return res; } };
标签:
原文地址:http://www.cnblogs.com/coderht/p/5462466.html