How this code find two nums from array which equal to the target?
This a Task from leetcode(Two sum)
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
And trere is a solution:
dict = {}
for i,n in enumerate(nums):
if n in dict:
return dict[n],i
dict[target-n]=i
How does it work? Describe this solution as good as you can,please.