Skip to contentKEEP THE CRAFTTHE SANCTUARY

Two Sum

Find two distinct indices whose values add to a target.

The problem

Implement Solution.twoSum(nums, target). Return the two distinct indices. Exactly one answer exists. The order of the two returned indices does not matter.

Starter code

class Solution:
    def twoSum(self, nums: list[int], target: int) -> list[int]:
        pass

Public examples

  1. nums = [2, 7, 11, 15], target = 9gives[0, 1]
  2. nums = [3, 2, 4], target = 6gives[1, 2]
  3. nums = [0, 8, 0], target = 0gives[0, 2]
Open it in the practice book
One thought at a time.