We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5c9fdf6 commit 0740ac6Copy full SHA for 0740ac6
โcontains-duplicate/river20s.py
@@ -0,0 +1,21 @@
1
+class Solution(object):
2
+ def containsDuplicate(self, nums):
3
+ """
4
+ :type nums: List[int]
5
+ :rtype: bool
6
+ nums ๋ฆฌ์คํธ์ ์ค๋ณต ๊ฐ์ด ๋์ค๋ฉด false,
7
+ ๋ฆฌ์คํธ์ ์ค๋ณต ๊ฐ์ด ์์ผ๋ฉด true๋ฅผ ๋ฐํํ๋ค.
8
+ - Time Complexity: O(n)
9
+ * ์ ์ฒด ๋ฐฐ์ด์ ํ ๋ฒ์ฉ ์ํํ๋ฉด์ ์ค๋ณต ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
10
+ - Space Complexity: O(n)
11
+ * Python์์ set()์ ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ํด์ ํ ์ด๋ธ์ ์ ์ฅํ๋ค.
12
+ ๋ฐฐ์ด์ ์ค๋ณต๋ ์์๊ฐ ์ ํ ์๋ค๋ฉด ๋ชจ๋ ์์(n๊ฐ)๋ฅผ set์ ์ ์ฅํ๊ฒ ๋๋ฏ๋ก
13
+ ๊ณต๊ฐ ๋ณต์ก๋๋ O(n)์ด ๋๋ค.
14
15
+ seen = set() # ์ค๋ณต ์ฌ๋ถ ํ์ธํ๋ set ๊ฐ์ฒด
16
+ for item in nums:
17
+ if item in seen:
18
+ return True
19
+ seen.add(item)
20
+ return False
21
+
0 commit comments