Skip to content

Commit 3e3a08e

Browse files
committed
feat: Add two-sum solutions
1 parent cd70330 commit 3e3a08e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

โ€Žtwo-sum/thispath98.pyโ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
"""
4+
Intuition:
5+
๊ธฐ์กด์— ํ’€์—ˆ๋˜ 3sum ๋ฌธ์ œ์™€ ์œ ์‚ฌํ•˜๊ฒŒ,
6+
ํ•ด์‹œ์— ํ˜„์žฌ ์ˆซ์ž์™€ ๋”ํ•ด์„œ target์ด ๋˜๋Š” ๊ฐ’์„ ์ฐพ๋Š”๋‹ค.
7+
๋งŒ์•ฝ ์—†์„ ๊ฒฝ์šฐ, ํ•ด์‹œ์— ํ˜„์žฌ ๊ฐ’๊ณผ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•œ๋‹ค.
8+
9+
Time Complexity:
10+
O(N):
11+
ํ•ด์‹œ๋Š” ์ ‘๊ทผํ•˜๋Š” ๋ฐ์— O(1)์ด ์†Œ์š”๋˜๊ณ ,
12+
์ด N๋ฒˆ ๋ฐ˜๋ณตํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N)์ด๋‹ค.
13+
14+
Space Complexity:
15+
O(N):
16+
์ตœ์•…์˜ ๊ฒฝ์šฐ ํ•ด์‹œ์— N๊ฐœ์˜ ์ˆซ์ž์™€ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค.
17+
"""
18+
complement_dict = {}
19+
for i, num in enumerate(nums):
20+
if target - num in complement_dict:
21+
return [complement_dict[target - num], i]
22+
else:
23+
complement_dict[num] = i

0 commit comments

Comments
ย (0)