File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ # ๋ฌธ์ ๋ด์ฉ
2+ # ๋ฐฐ์ด nums์ ๊ฐ์ ๊ฐ์ด 2๋ฒ ์ด์ ๋ฑ์ฅํ๋ฉด True, ์๋๋ฉด False.
3+ # ๊ฐ์ฅ ์ค์ ์ ์ธ ๋ฐฉ๋ฒ: ํด์์
(Set)์ผ๋ก ํ ๋ฒ์ฉ ๋ณด๋ฉฐ ๋ฑ์ฅ ์ฌ๋ถ ์ฒดํฌ
4+ # โ ์ด๋ฏธ ๋ณธ ๊ฐ์ด๋ฉด ๋ฐ๋ก True ๋ฐํ(์กฐ๊ธฐ ์ข
๋ฃ).
5+
6+ # ๋ฌธ์ ํ์ด ์ค๋ช
7+ # set์ ์ค๋ณต์ ํ์ฉํ์ง ์๋ ์งํฉ์ด๋ค
8+ # ๋ฆฌ์คํธ๋ฅด set์ผ๋ก ๋ฐ๊พธ๋ฉด ์ค๋ณต์ด ์ ๊ฑฐ๋๋ค
9+ # ๋ฐ๋ผ์ ๊ธธ์ด๋ฅผ ๋น๊ตํ๋ฉด ์ค๋ณต ์ฌ๋ถ๋ฅผ ์ ์ ์๋ค
10+
11+ # ์ค๋ณต์ด ์๋ ๊ฒฝ์ฐ
12+ # >>> nums = [1,2,3,2]
13+ # >>> len(nums)
14+ # 4
15+ # >>> set(nums)
16+ # {1, 2, 3}
17+ # >>> len(set(nums))
18+ # 3
19+ # >>> len(nums) != len(set(nums))
20+ # True
21+
22+ # ์ค๋ณต์ด ์๋ ๊ฒฝ์ฐ
23+ # >>> nums = [1,2,3]
24+ # >>> len(nums)
25+ # 3
26+ # >>> set(nums)
27+ # {1, 2, 3}
28+ # >>> len(set(nums))
29+ # 3
30+ # >>> len(nums) != len(set(nums))
31+ # False
32+ class Solution :
33+ def containsDuplicate (self , nums : List [int ]) -> bool :
34+ return len (nums ) != len (set (nums ))
35+
Original file line number Diff line number Diff line change 1+ # 1. ์ผ๋จ ๋์
๋๋ฆฌ(num_map)๋ฅผ ๋ง๋ค์ด์ ์ซ์๋ ์ธ๋ฑ์ค๋ฅผ ์ ์ฅํ๋ค.
2+ # 2. ํ์ฌ ์ซ์์ target์ ๋บ ๊ฒฐ๊ณผ(๋ณด์ถฉ๊ฐ, complement)๋ฅผ ๊ณ์ฐํ๋ค.
3+ # 3. ๊ทธ complement๊ฐ ์ด๋ฏธ ๋์
๋๋ฆฌ์ ์๋ค๋ฉด?
4+ # โ ๊ทธ ์ซ์์ ํ์ฌ ์ซ์๊ฐ ํฉ์ณ์ target์ด ๋๋ค๋ ๋ป!
5+ # 4. ์๋ค๋ฉด ํ์ฌ ์ซ์๋ฅผ ๋์
๋๋ฆฌ์ ์ ์ฅํด์ ๋ค์์ ๋๋นํ๋ค.
6+
7+ class Solution :
8+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
9+ num_map = {}
10+
11+ for i , num in enumerate (nums ):
12+ complement = target - num
13+
14+ if complement in num_map :
15+ return [num_map [complement ], i ]
16+ num_map [num ] = i
You canโt perform that action at this time.
0 commit comments