取巧用了python自带的排序算法,该算法为Tim sort,复杂度为nlog(n)
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if not nums:
return 0
nums.sort()
res = 0
length = 1
for i in range(len(nums)-1):
if nums[i] == nums[i+1]-1:
length += 1
elif nums[i] == nums[i+1]:
continue
else:
res = max(res,length)
length = 1
return max(res,length)