-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmazonTwoSums.py
30 lines (21 loc) · 1.24 KB
/
AmazonTwoSums.py
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
26
27
28
29
30
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
stored = {} #create map to store nums
for i in range(len(nums)) : #loop through array
complement = target - nums[i] #target = sum + target - sum so take complement
"""
check if complement present in map , no need of stored[complement] != i ,
i.e checking if complement is at same index : eg = [3,3] as we are doing one pass hashmap and storing data later in map.
"""
if complement in stored :
return [i, stored[complement]]. # if complement found return answer
else :
stored[nums[i]] = i # else add number to map stored
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
mapDict = {}
for i, n in enumerate(nums): # Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object. eg: (0, "egg") in [egg]
complement = target - n
if complement in mapDict.keys():
return[i, mapDict[complement]]
mapDict[n] = i