C# simple solution


  • 0
    M
    public IList<Interval> Merge(IList<Interval> intervals)
    {
    	var result = new List<Interval>();
    	if (intervals.Count == 0)
    		return result;
    
    	List<Interval> sorted = new List<Interval>(intervals);
    	sorted.Sort((x,y) => x.start - y.start);
    
    	var prev = sorted[0];
    
    	for (int i = 1; i < sorted.Count; i++)
    	{
    		var interval = sorted[i];
    		if (prev.end < interval.start)
    		{
    			result.Add(prev);
    			prev = interval;
    		}
    		else
    		{
    			prev = new Interval(prev.start, Math.Min(prev.end, interval.end));
    		}
    	}
    
    	result.Add(prev);
    
    	return result;
    }

Log in to reply
 

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