📒 Tech Note/알고리즘 & 자료구조
[LeetCode] 217. Contains Duplicate
@Hadev
2022. 8. 24. 22:57
*알고리즘 스터디에 참여하면서 Blind 75 LeetCode Questions 목록에 있는 문제를 풀이합니다.
문제: https://leetcode.com/problems/contains-duplicate/
Contains Duplicate - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
풀이:
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
# Sol 1 -- O(n)
# dist = {}
# for n in nums:
# if n in dist:
# return True
# dist[n] = 1
# return False
# Sol 2 -- Sorting O(N(logN))
# l = len(nums)
# if l < 2:
# return False
# nums.sort()
# for i in range(l-1):
# if nums[i] == nums[i+1]:
# return True
# return False
# Sol 3 -- Set Solution
numsSet = set(nums)
if len(nums) == len(numsSet):
return False
return True