-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2Sum.py
More file actions
20 lines (13 loc) · 662 Bytes
/
2Sum.py
File metadata and controls
20 lines (13 loc) · 662 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Link: https://leetcode.com/problems/two-sum/submissions/
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
ansDict = dict()
# Iterate list
for pos, i in enumerate(nums):
# Compute temporary result
result = target - i
# If complement exist inside dictionary, return key's value and current index
if i in ansDict.keys():
return [ansDict[i], pos]
# Add difference and current index to dictionary as key and value, respectively
ansDict[result] = pos