您的位置:首页 > 产品设计 > UI/UE

leetcodequestion_56 Merge Intervals

2013-09-27 19:55 344 查看
Given a collection of intervals, merge all overlapping intervals.

For example,
Given
[1,3],[2,6],[8,10],[15,18]
,
return
[1,6],[8,10],[15,18]
.

vector<Interval> merge(vector<Interval>& intervals) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(intervals.begin(), intervals.end(), cmp);
vector<Interval> res;
vector<Interval>::iterator it = intervals.begin();
Interval* cur = NULL;
for(it; it != intervals.end(); ++it)
{
if(cur == NULL)
{
cur = &(*it);
continue;
}
if((*cur).end < (*it).start)
{
res.push_back(*cur);
cur = &(*it);
}else{
(*cur).end = (*cur).end > (*it).end ? (*cur).end : (*it).end;
}
}
if(cur != NULL)
res.push_back(*cur);
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: