[LeetCode] Contains Duplicate

 

1. ๋ฌธ์ œ : Link

 

2. ํ’€์ด

๋‹จ์ˆœํžˆ ๋ฆฌ์ŠคํŠธ ํ™•์ธ์œผ๋กœ ํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์™€์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์—ˆ๋‹ค.

 

3. ์ฝ”๋“œ

sol1) ํ•ด์‰ฌ

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        tmp = {}
        for n in nums:
            if n in tmp:
                return True
            else:
                tmp[n] = 1
        return False

sol2) ๋ฆฌ์ŠคํŠธ ๊ธธ์ด

class Solution:
    def containsDuplicate_2(self, nums: List[int]) -> bool:
        return len(nums) != len(set(nums))
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ