156. 合并区间
vector<Interval> merge(vector<Interval> &intervals)
{
// 合并区间write your code here给出若干闭合区间,合并所有重叠的部分。Time Limit Exceeded
vector<Interval> result;
int n=intervals.size();
if(intervals.size()==0)return result;
if(intervals.size()==1)
{Interval tmp=intervals.back();result.push_back(tmp);return result;}
int i=0;
map<int,int> sortinter;
map<int,int>::iterator upper=sortinter.begin(),low=sortinter.begin(),i1=sortinter.begin();
sortinter.insert(pair<int,int>(intervals[0].start,intervals[0].end));
for(i=1;i<n;i++)
{
//pair<int,int> tmp(intervals[i].start,intervals[i].end);
upper=sortinter.begin();low=upper;
while(upper!=sortinter.end()&&(*upper).first<=intervals[i].end)
upper++;
if(upper!=sortinter.begin())
upper--;
while(low!=sortinter.end()&&(*low).second<intervals[i].start) low++;
if(low==sortinter.end())
{//判断Low--的条件需要琢磨,如果low不满足(*low).second<intervals[i].start那么不需要减,如果Low==begin()也不需要减,只有low已经出界时才需要减
low--;
sortinter[intervals[i].start]=intervals[i].end;
continue;
}
int tmpstart=intervals[i].start<(*low).first?intervals[i].start:(*low).first;
int tmpend=intervals[i].end>(*upper).second?intervals[i].end:(*upper).second;
if(upper==low)
{
if(intervals[i].end<(*upper).second &&intervals[i].start>(*upper).first)
{
continue;
}
if(intervals[i].start>(*upper).second)
{sortinter[intervals[i].start]=intervals[i].end;continue;}
if(intervals[i].end<(*low).first)
{sortinter[intervals[i].start]=intervals[i].end;continue;}
sortinter.erase(upper);
sortinter[tmpstart]=tmpend;
}
else
{
sortinter.erase(low,++upper);
sortinter[tmpstart]=tmpend;
}
}
i=0;
i1=sortinter.begin();
while(i<sortinter.size())
{
Interval tmp((*i1).first,(*i1).second);
i1++;i++;
result.push_back(tmp);
}
return result;
}
{
// 合并区间write your code here给出若干闭合区间,合并所有重叠的部分。Time Limit Exceeded
vector<Interval> result;
int n=intervals.size();
if(intervals.size()==0)return result;
if(intervals.size()==1)
{Interval tmp=intervals.back();result.push_back(tmp);return result;}
int i=0;
map<int,int> sortinter;
map<int,int>::iterator upper=sortinter.begin(),low=sortinter.begin(),i1=sortinter.begin();
sortinter.insert(pair<int,int>(intervals[0].start,intervals[0].end));
for(i=1;i<n;i++)
{
//pair<int,int> tmp(intervals[i].start,intervals[i].end);
upper=sortinter.begin();low=upper;
while(upper!=sortinter.end()&&(*upper).first<=intervals[i].end)
upper++;
if(upper!=sortinter.begin())
upper--;
while(low!=sortinter.end()&&(*low).second<intervals[i].start) low++;
if(low==sortinter.end())
{//判断Low--的条件需要琢磨,如果low不满足(*low).second<intervals[i].start那么不需要减,如果Low==begin()也不需要减,只有low已经出界时才需要减
low--;
sortinter[intervals[i].start]=intervals[i].end;
continue;
}
int tmpstart=intervals[i].start<(*low).first?intervals[i].start:(*low).first;
int tmpend=intervals[i].end>(*upper).second?intervals[i].end:(*upper).second;
if(upper==low)
{
if(intervals[i].end<(*upper).second &&intervals[i].start>(*upper).first)
{
continue;
}
if(intervals[i].start>(*upper).second)
{sortinter[intervals[i].start]=intervals[i].end;continue;}
if(intervals[i].end<(*low).first)
{sortinter[intervals[i].start]=intervals[i].end;continue;}
sortinter.erase(upper);
sortinter[tmpstart]=tmpend;
}
else
{
sortinter.erase(low,++upper);
sortinter[tmpstart]=tmpend;
}
}
i=0;
i1=sortinter.begin();
while(i<sortinter.size())
{
Interval tmp((*i1).first,(*i1).second);
i1++;i++;
result.push_back(tmp);
}
return result;
}