Two Sum
EasyInterview favoriteFreeYou get a list of whole numbers, nums, and a number target. Find the two numbers in the list that add up to target, and return their positions (their indexes).
Every input has exactly one answer, and you can't use the same position twice. Return the two indexes in any order.
Example 1
- Input
- nums = [2, 7, 11, 15], target = 9
- Output
- [0, 1]
nums[0] + nums[1] is 2 + 7, which makes 9.
Example 2
- Input
- nums = [3, 2, 4], target = 6
- Output
- [1, 2]
2 + 4 makes 6. 3 + 3 would also make 6, but it would use position 0 twice.
Example 3
- Input
- nums = [3, 3], target = 6
- Output
- [0, 1]
Two different positions can hold the same value.
Constraints
- 2 <= length of nums <= 300,000
- -1,000,000,000 <= nums[i] <= 1,000,000,000
- Exactly one pair adds up to target
Solve it in your browser
Write your solution in Python or JavaScript, run it on the examples, then submit it against hidden tests. 3 hints and worked solutions are there when you need them.