[LeetCode][Python3] 128. Longest Consecutive Sequence

티스토리 메뉴 펼치기 댓글수0

프로그래밍/LeetCode

[LeetCode][Python3] 128. Longest Consecutive Sequence

snoopybox
댓글수0

Problem :

https://leetcode.com/problems/longest-consecutive-sequence/


My Solution :

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
nums = set(nums)
ans = 0
for n in nums:
if n-1 not in nums:
m = n
while m in nums:
m += 1
ans = max(ans, m-n)
return ans


Comment :

개인적으로 set과 dictionary를 참 좋아한다. 탐색 시간이 O(1)이기 때문이다.

맨위로

https://www.snoopybox.co.kr/1967

신고하기