File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Solution :
5+ """
6+ Ideation:
7+ ๊ฐ ์์๊ฐ ๋ฐ๊ฒฌ๋๋ฉด hash map ์ ํด๋นํ๋ item ๊ธฐ๋ฐ์ผ๋ก flag๋ฅผ ์ธ์ด๋ค.
8+ ํด๋น ์ธ๋ฑ์ค์์ flag๊ฐ ์ด๋ฏธ ์ผ์ ธ์๋ค๋ฉด, ์์์ ํ์๋ ์์์ด๋ฏ๋ก True๋ฅผ ๋ฐํํ๋ค.
9+ ๋ง์ง๋ง๊น์ง ๋ค ๋์๋๋ฐ ์ค๋ณต๋ ์ผ์ด์ค๊ฐ ์๋ค๋ฉด False๋ฅผ ๋ฐํํ๋ค.
10+ Time complexity: O(n)
11+ Space complexity: O(n)
12+ """
13+
14+ def containsDuplicate (self , nums : List [int ]) -> bool :
15+ seen = {}
16+ for num in nums :
17+ if num in seen :
18+ return True
19+ seen [num ] = 1
20+ return False
Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Solution :
5+ """
6+ Ideation:
7+ target = x + y -> target - x = y
8+ x ์์๋ฅผ ๊ฐ์ฅ ๋ฐ๊นฅ์ ์ดํฐ๋ ์ด์
์์ ๋๋ฉด์ ํ๋์ฉ ๋์
ํฉ๋๋ค.
9+ ์ฐพ๊ณ ์ ํ๋ ๊ฐ์ด (x,y) ์์ด๋ฏ๋ก, ๋๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ์ฐพ๊ธฐ ์ํด ์ธ๋ฑ์ค ๊ธฐ์ค์ผ๋ก ์ดํฐ๋ ์ด์
์ ์ํํฉ๋๋ค( enumerate ์ฌ์ฉ ๊ฐ๋ฅ).
10+ y๊ฐ์ด ์ฐพ์์ง๋ฉด ๋น์์ x๊ฐ์ ์ธ๋ฑ์ค์ ํจ๊ป (index_of_x, index_of_y) ๋ฅผ ๋ฆฌ์คํธ๋ก ๋ฐํํฉ๋๋ค.
11+ Time Complexity: O(N^2)
12+ Space Complexity: O(1)
13+ """
14+
15+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
16+ for i in range (0 , len (nums ) - 1 ):
17+ for j in range (i + 1 , len (nums )):
18+ if (target - nums [i ]) == nums [j ]:
19+ return [i , j ]
You canโt perform that action at this time.
0 commit comments