Runtime error, ok in vs, anybody know why?


  • 0
    S

    for test case [[1,5]], [6,8],anybody know why? thanks in advance

        vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) 
    {
    	if (intervals.size() == 0)
    	{
    		intervals.push_back(newInterval);
    		return intervals;
    	}
    	
    	int start = newInterval.start;
    	int end = newInterval.end;
    
    	if (end < intervals.front().start)
    	{
    		intervals.insert(intervals.begin(), newInterval);
    		return intervals;
    	}
    
    	if (start > intervals.back().end)
    	{
    		intervals.push_back(newInterval);
    		return intervals;
    	}
    
    
    	int idxStart = -1;
    	int idxEnd = -1;
    
    	for (int i = 0; i < intervals.size(); i++)
    	{
    		if (intervals[i].start < start && intervals[i].end > start) //落在某个区间上
    		{	
    			idxStart = i;
    			break;
    		}
    		else if (intervals[i].start > start) //落在区间外
    		{
    			if (intervals[i].start > end)
    			{
    				intervals.insert(intervals.begin() + i, newInterval);
    				return intervals;
    			}
    			else
    			{
    				intervals[i].start = start;
    				idxStart = i;
    				break;
    			}
    		}
    	}
    
    
    	for (int i = 0; i < intervals.size(); i++)
    	{
    		if (intervals[i].start < end && intervals[i].end > end) //落在某个区间上
    		{	
    			idxEnd = i;
    			break;
    		}
    		else if (intervals[i].start > end) //落在区间外
    		{
    			intervals[i-1].end = end;
    			idxEnd = i - 1;
    			break;
    		}
    		else if (i == intervals.size()-1)
    		{
    			intervals[i].end = end;
    			idxEnd = i;
    			break;
    		}
    	}
    
    	intervals[idxEnd].start = intervals[idxStart].start;
    	intervals.erase(intervals.begin() + idxStart, intervals.begin() + idxEnd);
    
        return intervals;
    }

Log in to reply
 

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