Two Sum — Arrays & Hashing
Find two different positions in a list of integers whose values add up to a chosen target, and report where they are.
Arrays & Hashing · Easy
The problem
Given integers and a target, find the two different positions whose values add up to the target. Exactly one pair works.
What to return
- Return [i, j], the 0-based indices of the two numbers that add up to target.
- The two indices must be different. Any order is accepted.
- Large hidden tests (up to 400 numbers) run under a step budget of executed lines.
Constraints
- 2 ≤ len(nums) ≤ 400
- −10⁴ ≤ nums[i], target ≤ 10⁴
- Exactly one pair of indices adds up to target.
- Your code may run at most 4,756 counted steps on the largest case.
Example
- Input
nums = [2, 7, 11, 15], target = 9- Output
[0, 1]