-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.py
More file actions
26 lines (21 loc) · 787 Bytes
/
TwoSum.py
File metadata and controls
26 lines (21 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Difficulty: Easy
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""
The idea here is that for every element x at index i in the list,
we store `target-x` in a dictionary as the key and i as the value.
If for an index i, target-x already exists in the dictionary, we
return i and the value of the dictionary
args:
nums: List of numbers
target: The target value
return:
list(size=2) of indices which sum to the target
"""
indexMap = {}
for i, val in enumerate(nums):
if target - val in indexMap.keys():
return [i, indexMap[target - val]]
else:
indexMap[val] = i