mycode 92.62%
class Solution(object): def longestConsecutive(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return 0 nums = sorted(set(nums)) res = 1 final = 1 for i in range(len(nums)-1): print(nums[i],nums[i+1]) if nums[i+1] == (nums[i] + 1): res += 1 else: final = max(final,res) res = 1 final = max(final,res) return final
参考
class Solution(object): def longestConsecutive(self, nums): """ :type nums: List[int] :rtype: int """ nums=set(nums) maxi=0 for i in nums: if i-1 not in nums: y=i+1 while y in nums: y=y+1 maxi=max(maxi,y-i) return maxi