Two Sum
Part of Practice: Hashing Warmups
Given an array of integers nums and an integer target, find the two different positions whose values add up to target, and return their indexes. Exactly one such pair exists, and you may not use the same element twice.
The array can hold up to 10,000 numbers and is not sorted, and the answer can be returned in any order.
Examples
- Input:
- nums = [2, 7, 11, 15], target = 9
- Output:
- [0, 1]
nums[0] + nums[1] = 2 + 7 = 9.
- Input:
- nums = [3, 2, 4], target = 6
- Output:
- [1, 2]
2 + 4 = 6. Note [0, 0] is not allowed — you cannot use the 3 twice.
- Input:
- nums = [3, 3], target = 6
- Output:
- [0, 1]
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.