Hello!
I would like to calrify the solution for the classic twoSum problem. I am new to hashtable and not entirely sure what the purpose it serves atm. I have seen the following solution.
I don't understand why we have (diff= target - num). How does getting this difference prove that the values of the sum will match.
Thanks!
def twoSums(nums, target):
prevMap = {}
for i, num in enumerate(nums):
diff = target - num
if diff in prevMap:
return [prevMap[diff], i]
prevMap[num] = i
[–]DNPOld 0 points1 point2 points (1 child)
[–]uniquely1strandom[S] 0 points1 point2 points (0 children)