Example 1
Input
nums = [2, 7, 11, 15], target = 9
Output
[0, 1]
The values at indices 0 and 1 add to 9.
Loading CodeSprint...
/problems/two-sum
Given a list of integers and a target value, return the indices of two different positions whose values add to the target. Each input has exactly one valid pair.
Implement this function and return the result. CodeSprint supplies each test case automatically.
twoSum(nums, target) → number[]Input
nums = [2, 7, 11, 15], target = 9
Output
[0, 1]
The values at indices 0 and 1 add to 9.
Input
nums = [3, 2, 4], target = 6
Output
[1, 2]
Constraints
2 <= nums.length <= 10,000 -1,000,000 <= nums[i], target <= 1,000,000 Exactly one answer exists.