# I want to know what's wrong with my code.

• First, using bubble sort to sort the intervals with Interval.start sorting from small to big.

And then do the merge. But there is something wrong with my code. I need someone' help.

``````class Solution {
public:
vector<Interval> merge(vector<Interval>& intervals) {
bool have_swapped = true;
for(unsigned j = 1; have_swapped && j < intervals.size(); ++j)
{
have_swapped = false;
for(vector<Interval>::iterator it = intervals.begin(); it != intervals.end(); it++)
{
if(it->start > (it+1)->start)
{
have_swapped = true;
vector<Interval>::iterator i = it;
it = it+1;
it+1 = i;
}
}
}
vector<Interval> interval2;
for(vector<Interval>::iterator it = intervals.begin(); it != intervals.end(); ++it)
{
if(it->end >= (it+1)->start && it->end <= (it+1)->end)
{
it->end = (it+1)->end;
interval2.push_back(*it);
}
else if(it->end >= (it+1)->start && it->end >= (it+1)->end)
{
interval2.push_back(*it);
}
else if(it->end <= (it+1)->start)
interval2.push_back(*it);
}
return interval2;
}
};``````

• Assume your sorting is correct.
You should not start pushing merged Interval into answer vector when *it and *(it+1) (you should also validate (it + 1))has overlap. Imagine Intervals like [1,4] [2,4] [3,5], you code outputs [1,4], [2,5]

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.