File tree Expand file tree Collapse file tree 1 file changed +12
-26
lines changed Expand file tree Collapse file tree 1 file changed +12
-26
lines changed Original file line number Diff line number Diff line change 1
1
"""
2
- Title: 217. Contains Duplicate
3
- Link: https://leetcode.com/problems/contains-duplicate/
2
+ Problem: 217. Contains Duplicate
4
3
5
- Summary:
6
- - ์ฃผ์ด์ง ๋ฐฐ์ด `nums`์์ ์ด๋ค ๊ฐ์ด ํ ๋ฒ ์ด์ ๋ฑ์ฅํ๋ฉด True๋ฅผ ๋ฐํํ๊ณ , ๋ฐฐ์ด์ ๋ชจ๋ ๊ฐ์ด ์ ์ผํ ๊ฒฝ์ฐ์๋ False๋ฅผ ๋ฐํํจ
7
- - Input: `nums = [1,2,3,1]`
8
- - Output: `True`
4
+ Constraints:
5
+ - 1 <= nums.length <= 10^5
6
+ - -10^9 <= nums[i] <= 10^9
9
7
10
- Conditions:
11
- - ์ค๋ณต์ด ์์ผ๋ฉด: ๋ฐฐ์ด์์ ์ ์ด๋ ํ๋์ ๊ฐ์ด ๋ ๋ฒ ์ด์ ๋ฑ์ฅํ๋ฉด `True` ๋ฐํ
12
- - ์ค๋ณต์ด ์์ผ๋ฉด: ๋ฐฐ์ด์ ๋ชจ๋ ๊ฐ์ด ์ ์ผํ๋ฉด `False` ๋ฐํ
13
- """
8
+ Time Complexity: O(n)
9
+ - ๋ฐฐ์ด์ ํ ๋ฒ๋ง ์ํํจ
10
+ - ์งํฉ์์ ๊ฒ์๊ณผ ์ถ๊ฐ ์ฐ์ฐ์ ํ๊ท ์ ์ผ๋ก O(1)
11
+ - ์ด n๊ฐ ์์์ ๋ํด ๊ฐ๊ฐ O(1) ์ฐ์ฐ ์ํ
14
12
15
- """
16
- First Try
17
- Time Complexity:
18
- - O(n) * O(n) = O(n^2): `for` ๋ฃจํ์์ `nums.count(i)`๋ฅผ ํธ์ถํ ๋๋ง๋ค ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฏ๋ก, ์ ์ฒด ์๊ฐ ๋ณต์ก๋๋ `O(n^2)`
19
- """
20
- class Solution :
21
- def containsDuplicate (self , nums : List [int ]) -> bool :
22
- for i in nums :
23
- if nums .count (i ) > 1 :
24
- return True
25
- return False
26
-
27
- """
28
- Second Try (set๋ฅผ ํ์ฉํ์ฌ ์ด๋ฏธ ๋ณธ ์์๋ฅผ ํจ์จ์ ์ผ๋ก ์ถ์ ํ๋ ๋ฐฉ๋ฒ)
29
- Time Complexity:
30
- - O(n): `for` ๋ฃจํ์์ ๊ฐ ์ซ์์ ๋ํด `in` ์ฐ์ฐ๊ณผ `add` ์ฐ์ฐ์ด ์์ ์๊ฐ O(1)์ผ๋ก ์ฒ๋ฆฌ๋๋ฏ๋ก, ์ ์ฒด ์๊ฐ ๋ณต์ก๋๋ O(n)
13
+ Space Complexity: O(n)
14
+ - ์ต์
์ ๊ฒฝ์ฐ ๋ชจ๋ ์์๋ฅผ ์งํฉ์ ์ ์ฅ
15
+ - ์ถ๊ฐ ๊ณต๊ฐ์ด ์
๋ ฅ ๋ฐฐ์ด ํฌ๊ธฐ์ ๋น๋กํจ
31
16
"""
32
17
class Solution :
33
18
def containsDuplicate (self , nums : List [int ]) -> bool :
34
19
seen = set ()
20
+
35
21
for i in nums :
36
22
if i in seen :
37
23
return True
You canโt perform that action at this time.
0 commit comments