Python solution


  • 0
    R

    class SummaryRanges(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.h={}
    
    def addNum(self, val):
        """
        :type val: int
        :rtype: void
        """
        keys=self.h.keys()
        merge=[]
        for e in keys:
            if e[0]<=val and val<=e[1]:
                return
            elif e[0]-1==val or e[1]+1==val:
                merge.append(e)
        
        
        if merge:
            merge.sort()
            start=min(merge[0][0],val)
            end=max(merge[-1][1],val)
            for e in merge:
                del self.h[e]
            self.h[(start,end)]=Interval(start,end)
        else:
            self.h[(val,val)]=Interval(val,val)
            
        return        
        
    
    def getIntervals(self):
        """
        :rtype: List[Interval]
        """
        return [ self.h[x] for x in sorted(self.h.keys())]

Log in to reply
 

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