diff --git a/contains-duplicate/doh6077.py b/contains-duplicate/doh6077.py index bc14e47f24..11a03c3ca6 100644 --- a/contains-duplicate/doh6077.py +++ b/contains-duplicate/doh6077.py @@ -1,10 +1,7 @@ +# https://leetcode.com/problems/contains-duplicate/description/ -# set에 저장하면서 중복 여부 확인하기 class Solution: - def containsDuplicate(self, nums: list[int]) -> bool: - hashset = set() - for i in nums: - if i in hashset: - return True - hashset.add(i) - return False + def containsDuplicate(self, nums: List[int]) -> bool: + # Create a set to store unique numbers from nums + nums_set = set(nums) + return len(nums_set) != len(nums) diff --git a/two-sum/doh6077.py b/two-sum/doh6077.py index 2054ef0def..d8c8a3683a 100644 --- a/two-sum/doh6077.py +++ b/two-sum/doh6077.py @@ -1,8 +1,24 @@ +# 6기 +# class Solution: +# def twoSum(self, nums: list[int], target: int) -> list[int]: +# prevMap = {} # val : index +# for i, n in enumerate(nums): +# diff = target - n +# if diff in prevMap: +# return [prevMap[diff], i] +# prevMap[n] = i + +# 7기 +# https://leetcode.com/problems/two-sum/description/ class Solution: - def twoSum(self, nums: list[int], target: int) -> list[int]: - prevMap = {} # val : index - for i, n in enumerate(nums): - diff = target - n - if diff in prevMap: - return [prevMap[diff], i] - prevMap[n] = i + def twoSum(self, nums: List[int], target: int) -> List[int]: + # use Hash map to save num and index + nums_hm = {} + + for i, num in enumerate(nums): + find_val = target - num + + if find_val in nums_hm: + return [nums_hm[find_val], i] + + nums_hm[num] = i