File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ # Constraints
3+
4+ 2 <= nums.length <= 10^4
5+ -10^9 <= nums[i] <= 10^9
6+ -10^9 <= target <= 10^9
7+
8+ ๊ฐ์ ์ซ์ ๋๋ฒ ์ฌ์ฉ X
9+
10+ # Time Complexity: O(n)
11+
12+ O(n^2) ์ผ๋ก๋ ๊ฐ๋ฅ์ ํ์ง๋ง, ๋ฌธ์ ์์ ์ด๊ฑฐ๋ณด๋ค ๋ ์ ์ ์๊ฐ๋ณต์ก๋๋ก ํ ์๋ ์๋ค๋ ๋ง์ด ์กด์ฌ
13+ ๋ฐฐ์ด ์ํํ๋ฉฐ
14+ ์ฒซ ์์๋ target - n ์ ํ ๊ฐ์ set์ ๋ฃ์ด๋๊ธฐ
15+ ๋ค์ ์์๋ถํฐ๋ ํด๋น ๊ฐ์ด set์ ์๋์ง ์ฒดํฌ
16+ ์๋ค๋ฉด target - n ๋ฃ๊ธฐ
17+
18+ ๊ทผ๋ฐ ์๊ฐํด๋ณด๋ ์์์ "idx" ๋ฅผ ๋ฐํํด์ผํจ
19+ -> set๋์ dict๋ฅผ ์จ์,
20+ -> dict[value] = idx๋ก ๊ตฌ์ฑ
21+ -> dict[7] = 0
22+ -> ๋ฐฐ์ด 7 ์ค๋ฉด, ํ์ฌ idx๋ ๊ธฐ์กด idx ๊ฐ์ ธ์ค๊ธฐ ๊ฐ๋ฅ!
23+
24+ # Space Complexity: O(n)
25+
26+ ๋ฐฐ์ด ์์ ๊ฐ์๋งํผ target - n ๊ฐ์ด ๋ค์ด๊ฐ set ํฌ๊ธฐ๊ฐ ๊ฒฐ์ ๋จ
27+
28+ """
29+
30+
31+ class Solution :
32+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
33+ ret = []
34+ diffDict = dict ()
35+
36+ diffDict [target - nums [0 ]] = 0
37+
38+ for i in range (1 , len (nums )):
39+ if nums [i ] in diffDict :
40+ ret .append (diffDict [nums [i ]])
41+ ret .append (i )
42+ return ret
43+
44+ else :
45+ diffDict [target - nums [i ]] = i
You canโt perform that action at this time.
0 commit comments