Python again


  • 0
    X
    class Solution(object):
        def longestConsecutive(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            ss = []
            for j in range(len(nums)):
                if nums[j] not in ss:
                    ss.append(nums[j])
                else:
                    continue
            ss.sort()
            l = len(ss)
            c = 1
            s = []
            for i in range(l-1):
                if ss[i+1]-ss[i]==1:
                    c += 1
                    s.append(c)
                else:
                    s.append(c)
                    c = 1
            if s:
                length = max(s)
            else:
                length = 1
            return length
    

  • 0
    R

    This solution is not even close to O(n).
    The " if nums[j] not in ss" line makes it O(n**2), and "ss.sort()" is O(n log n).


Log in to reply
 

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