Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target. Tags: Array
Try It!
Discussion
Video
Solution
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
target_dif = {}
for i in range(len(nums)):
num = nums[i]
if num in target_dif:
return [target_dif[num], i]
else:
dif = target - num
target_dif[dif] = i