[LeetCode][Python3] 128. Longest Consecutive Sequence
2019. 3. 23. 01:03 |
프로그래밍/LeetCode
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)이기 때문이다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 334. Increasing Triplet Subsequence (0) | 2019.03.25 |
---|---|
[LeetCode][Python3] 297. Serialize and Deserialize Binary Tree (0) | 2019.03.25 |
[LeetCode][Python3] 105. Construct Binary Tree from Preorder and Inorder Traversal (0) | 2019.03.24 |
[LeetCode][Python3] 131. Palindrome Partitioning (0) | 2019.03.23 |
[LeetCode][Python3] 42. Trapping Rain Water (0) | 2019.03.23 |
[LeetCode][Python] 341. Flatten Nested List Iterator (0) | 2019.03.22 |
[LeetCode][Python3] 200. Number of Islands (0) | 2019.02.11 |
[LeetCode][Python3] 300. Longest Increasing Subsequence (0) | 2019.02.10 |
최근에 달린 댓글 최근에 달린 댓글