Skip to content

Commit 1e37b25

Browse files
committed
Solve: two-sum
1 parent f310a52 commit 1e37b25

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

two-sum/hj4645.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
# 2개의 수를 합해 target이 되는 경우를 찾는 문제
3+
# 순서가 보장되는 python dictionary를 사용해서,
4+
# 요소 x에 대해서 target-x 가 딕셔너리 내에 있는지를 찾는다.
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
dict = {}
7+
for i, num in enumerate(nums):
8+
remain = target - num
9+
if remain in dict:
10+
return [dict[remain], i]
11+
dict[num] = i
12+

0 commit comments

Comments
 (0)