Question Link :Click Here
Python Code:
class Solution:
def longestConsecutive(self, num: List[int]) -> int:
s = set(num)
s = sorted(s)
nums = s
count = 1
result = 1
if not len(nums)==0:
prev = nums[0]
for i in range(1,len(nums)):
if nums[i] == prev + 1:
count +=1
prev = nums[i]
else:
result = max(count,result)
count = 1
prev = nums[i]
if i == len(nums) - 1:
result = max(count,result)
return result
else:
return 0
Python Code:
class Solution:
def longestConsecutive(self, num: List[int]) -> int:
s = set(num)
s = sorted(s)
nums = s
count = 1
result = 1
if not len(nums)==0:
prev = nums[0]
for i in range(1,len(nums)):
if nums[i] == prev + 1:
count +=1
prev = nums[i]
else:
result = max(count,result)
count = 1
prev = nums[i]
if i == len(nums) - 1:
result = max(count,result)
return result
else:
return 0
0 Comments