File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ # https://leetcode.com/problems/contains-duplicate/
2
+
3
+ from typing import List
4
+
5
+ class Solution :
6
+ def containsDuplicate1 (self , nums : List [int ]) -> bool :
7
+ """
8
+ [Complexity]
9
+ - TC: O(n) (set(nums) ์ ์์๋ฅผ ๋ณต์ฌํ๋ ๋ฐ์ O(n), len()์ O(1))
10
+ - SC: O(n)
11
+
12
+ [Approach]
13
+ set(hash map)์ ์ด์ฉํ์ฌ ์ค๋ณต์ ์ ๊ฑฐํ ๊ฒฐ๊ณผ์ ๊ธธ์ด๋ฅผ ์๋ณธ ๋ฆฌ์คํธ์ ๊ธธ์ด์ ๋น๊ตํ๋ฉด ๋๋ค.
14
+ """
15
+
16
+ return len (nums ) != len (set (nums ))
17
+
18
+
19
+ def containsDuplicate (self , nums : List [int ]) -> bool :
20
+ """
21
+ [Complexity]
22
+ - TC: O(n) (iteration)
23
+ - SC: O(n)
24
+
25
+ [Approach]
26
+ nums๋ฅผ ์ํํ๋ฉด์ set(hash map)์ ๋ฑ์ฅํ๋ ์์๋ฅผ add ํ๊ณ , ๋์ผํ ์์๊ฐ ๋ฐ๊ฒฌ๋๋ฉด True๋ฅผ ๋ฐํํ๋ค.
27
+ nums ์ ์ฒด๋ฅผ ์ํํ๋ฉด์ ๋์ผํ ์์๊ฐ ์์๋ค๋ฉด False๋ฅผ ๋ฐํํ๋ค.
28
+ """
29
+
30
+ seen = set ()
31
+
32
+ for num in nums : # -- O(n)
33
+ if num in seen : # -- O(1)
34
+ return True
35
+ seen .add (num )
36
+
37
+ return False
You canโt perform that action at this time.
0 commit comments