This is my code (time usage 42ms) using python


  • 0
    H
    class Solution(object):
        def longestConsecutive(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            nums_dict = {}                                                              
            for num in nums:                                                            
                nums_dict[num] = False                                                  
            length = len(nums)                                                          
            longestLen = 0                                                              
            i = 0                                                                       
            while i < length:                                                           
                if nums_dict[nums[i]]:                                                  
                    i = i + 1                                                           
                    continue                                                            
                v = nums[i]                                                             
                tmpLen = 1                                                              
                nums_dict[nums[i]] = True                                               
                v1 = v + 1                                                              
                v2 = v - 1                                                              
                while True:                                                             
                    if v1 in nums_dict and not nums_dict[v1]:                           
                        nums_dict[v1] = True                                            
                        tmpLen = tmpLen + 1                                             
                        v1 = v1 + 1                                                     
                    else:                                                               
                        break   
                while True:                                                             
                    if v2 in nums_dict and not nums_dict[v2]:                           
                        nums_dict[v2] = True                                            
                        tmpLen = tmpLen + 1                                          
                        v2 = v2 - 1                                                  
                    else:                                                            
                        break                                                        
                longestLen = max(longestLen, tmpLen)                                 
                i = i + 1                                                            
            return longestLen
    

Log in to reply
 

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