128 最长连续数列

128 最长连续数列

  • 题目链接
    https://leetcode-cn.com/problems/longest-consecutive-sequence/
  • 代码
class Solution:
    def longestConsecutive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums = set(nums)
        res = 0
        for i in nums:
            if i-1 not in nums:
                y = i +1
                while y in nums:
                    y += 1
                res = max(res, y - i)
        return res

  • 总结
    由于最近时间原因,先借鉴劈里啪啦大佬的代码,自己有时间去研究一下。
    https://blog.csdn.net/starmoth/article/details/88427988

你可能感兴趣的:(128 最长连续数列)