//风格比较差,用时较长:712ms
1 /** 2 * Definition for an interval. 3 * struct Interval { 4 * int start; 5 * int end; 6 * Interval() : start(0), end(0) {} 7 * Interval(int s, int e) : start(s), end(e) {} 8 * }; 9 */ 10 class Solution; 11 bool incmp(struct Interval a,struct Interval b) 12 { 13 return a.start<b.start; 14 } 15 class Solution { 16 public: 17 18 void mer(vector<Interval> &v,int &i) 19 { 20 if(v[i+1].start<=v[i].end) 21 { 22 if(v[i].end<v[i+1].end) 23 v[i].end=v[i+1].end; 24 v.erase((vector<Interval>::iterator)(&v[i+1])); 25 i--; 26 } 27 } 28 vector<Interval> merge(vector<Interval> &intervals) { 29 // Start typing your C/C++ solution below 30 // DO NOT write int main() function 31 if(intervals.size()<2) 32 return intervals; 33 sort(intervals.begin(),intervals.end(),incmp); 34 35 for(int i=0;i<intervals.size()-1;i++) 36 { 37 mer(intervals,i); 38 } 39 return intervals; 40 } 41 };