// struct Interval { // int start; // int end; // Interval(int s, int e) : start(start), end(e) {} // }; #include <vector> #include<algorithm> bool cmp(Interval a,Interval b){ return a.start<b.start; } class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param intervals Interval类vector * @return Interval类vector */ vector<Interval> merge(vector<Interval>& intervals) { // write code here vector<Interval> ans; if(intervals.empty()){ return ans; } if(intervals.size()==1){ return intervals; } sort(intervals.begin(),intervals.end(),cmp); int start=intervals[0].start; int end=intervals[0].end; for(int i=1;i<intervals.size();i++){ if(intervals[i].start<=end){ if(intervals[i].end>end){ end=intervals[i].end; continue; } } else{ ans.push_back(Interval(start,end)); start=intervals[i].start; end=intervals[i].end; continue; } } ans.push_back(Interval(start,end)); return ans; } };