128. Longest Consecutive Sequence刷题笔记
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
取巧用了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)
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |