gumgum's Garden🌼

2 Sum problem

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
Questions
  1. https://leetcode.com/problems/two-sum/