Python, not concise enough


  • 0
    2
    def summaryRanges(self, nums):
        res = []
        tmp = ''
        
        for i in nums:
            if not tmp:
                tmp = str(i)
            elif '->' not in tmp and i == int(tmp) + 1:
                tmp += '->' + str(i)
            elif '->' in tmp and i == int(tmp.split('->')[1]) + 1:
                tmp = tmp.split('->')[0] + '->' + str(i)
            else:
                res.append(tmp)
                tmp = ''
                tmp = str(i)
        if tmp:
            res.append(tmp)
                
        return res

Log in to reply
 

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