Two Sum
easy · hashmap
You are given a list of integers `nums` and an integer `target`. Find two different positions in the list whose values add up to `target`, and return their indices as a list `[i, j]`. There is exactly one pair that works, and you may not use the same position twice.
Input: `nums` (a list of integers) and `target` (an integer).
Output: a list of two indices.
Example: nums = [2, 7, 11, 15], target = 9 -> [0, 1], because nums[0] + nums[1] = 2 + 7 = 9.
Starter code
def two_sum(nums: list[int], target: int) -> list[int]:
"""Return the indices of the two numbers that add up to target."""
pass
Enable JavaScript to edit this code and run the hidden tests in your browser. Each test has a five-second execution limit. Your drafts and solved progress stay in this browser.
Return to the Python compiler