Skip to content

Commit fe8e98e

Browse files
committed
feat: add containsDuplicate function to check for duplicate numbers in a list
1 parent b7c1554 commit fe8e98e

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
"""
4+
๋ฆฌ์ŠคํŠธ์— ์ˆซ์ž๊ฐ€ ๋‘๋ฒˆ ์ด์ƒ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฉด True,
5+
๋ชจ๋“  ์ˆซ์ž๊ฐ€ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋“ฑ์žฅํ•˜๋ฉด (unique) False๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
6+
7+
๋ฐฉ๋ฒ•:
8+
1. ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋™์ผํ•œ ์ˆซ์ž๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ• (with dict); o(n) ์‹œ๊ฐ„๋ณต์žก๋„
9+
2. ๋ฆฌ์ŠคํŠธ๋ฅผ set์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๊ธธ์ด๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•; o(n) ์‹œ๊ฐ„๋ณต์žก๋„ -> PICK!
10+
3. ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•œ ํ›„ ์ธ์ ‘ํ•œ ์ˆซ์ž๊ฐ€ ๋™์ผํ•œ์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•; o(nlogn) ์‹œ๊ฐ„๋ณต์žก๋„
11+
12+
* ๊ธธ์ด๊ฐ€ 1์ผ ๊ฒฝ์šฐ, ์ค‘๋ณต์ด ์žˆ์„ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
13+
14+
๋ฐฉ๋ฒ•(2)๊ฐ€ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ณ  ํšจ์œจ์ ์ด์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์€ ๊ธธ์ด๊ฐ€ n์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ set์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ O(n) ๋งŒํผ ํ•„์š”ํ•˜๋‹ค.
15+
๋ฐฉ๋ฒ•(3)์€ ์ถ”๊ฐ€์ ์ธ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์ง€๋งŒ, ์ •๋ ฌํ•  ๋•Œ O(nlogn) ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋“ ๋‹ค.
16+
17+
Args:
18+
nums (List[int]): ๋™์ผํ•œ ์ˆซ์ž๋ฅผ ํฌํ•จํ•œ ์ •์ˆ˜ ๋ฐฐ์—ด
19+
20+
Returns:
21+
bool: ์ค‘๋ณต์ด ์—†์œผ๋ฉด False, ์žˆ์œผ๋ฉด True
22+
"""
23+
if len(nums) <= 1:
24+
return False
25+
return len(nums) != len(set(nums))

0 commit comments

Comments
ย (0)