Given an array of integersĀ nums
Ā and an integerĀ target
, returnĀ indices of the two numbers such that they add up toĀ target
.
def twoSum(self, nums: List[int], target: int) -> List[int]:
pre = {}
for i in range(len(nums)):
num = nums[i]
if target - num in pre:
return [pre[target - num], i]
if num not in pre:
pre[num] = i
return