题目链接:https://www.nowcoder.com/practice/69f4e5b7ad284a478777cb2a17fb5e6a?tpId=196&rp=1&ru=%2Fexam%2Foj&qru=%2Fexam%2Foj&sourceUrl=%2Fexam%2Foj%3Fpage%3D2%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=&judgeStatus=&tags=&title=37&gioEnter=menu
题目如下:
class Solution {
public:
static bool cmp(Interval &a,Interval &b){
if(a.start==b.start) return a.end merge(vector &intervals) {
vector res;
if(intervals.size()==0) return res;
else if(intervals.size()==1) {res.push_back(intervals[0]);return res;}
sort(intervals.begin(),intervals.end(),cmp);//类型不同。要用cmp
Interval temp;
int l=intervals[0].start,r=intervals[0].end;
for(int i=1;i
if(r>=intervals[i].start){//需要合并
r=max(r,intervals[i].end);
//temp.start=l;temp.end=r;
//res.push_back(temp);
//上面两行不对,因为此时不确定后面一个区间是否要和当前这个区间合并
}else {//先保存独立的区间,再更新左右指针
temp.start=l;temp.end=r;
res.push_back(temp);
l=intervals[i].start;
r=intervals[i].end;
}
}
temp.start=l;temp.end=r;
res.push_back(temp);
return res;
}
};



