Two Sum
Problem statement
Return the two distinct indices in nums whose values sum to target. Exactly one valid pair exists, and the same array position cannot be used twice.
Return the smaller index first.
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [2,7,11,15]target = 9return = [0,1]The values at positions 0 and 1 sum to 9.
Example 2
nums = [3,2,4]target = 6return = [1,2]Use the values 2 and 4.
Example 3
nums = [3,3]target = 6return = [0,1]Equal values are allowed when they come from different positions.
Constraints
2 <= nums.length <= 10^4.-10^9 <= nums[i], target <= 10^9.- Exactly one pair of distinct indices has the required sum.