Python: Sorted Set of Nums


  • 0
    D

    My solution if anyone is having any difficulties. Pretty straight forward, but if you need help let me know!

    class Solution(object):
        def longestConsecutive(self, nums):
            nums=sorted(list(set(nums)))
            max = 1
            current = 1
            print nums
            if len(nums)==0:
                return 0
            if len(nums)==1:
                return 1
            for i in range(0,len(nums)-1):
                if nums[i] == nums[i+1]-1 or nums[i]==nums[i+1]-1:
                    current+=1
                    if current > max:
                        max = current
                else:
                    if current > max:
                        max = current
                        current = 1
                    else:
                        current = 1
            return max
    

Log in to reply
 

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