sum 1


class Solution:
  def twoSum(self, nums: List[int], target: int) -> List[int]:
    numToIndex = {}

    for i, num in enumerate(nums):
      if target - num in numToIndex:
        return numToIndex[target - num], i
      numToIndex[num] = i

Here is what the above code is Doing:
1. We create a dictionary called numToIndex.
2. We loop through the nums list.
3. For each number, we check if target – num is in numToIndex.
4. If it is, we return the index of target – num and the current index.
5. If it isn’t, we add the current number and its index to numToIndex.

Similar Posts