Two Sum
Problem statement
Return two distinct zero-based indices [i,j] such that i < j and nums[i] + nums[j] == target.
If several pairs work, choose the smallest first index, then the smallest second index. If none work, return [-1,-1].
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [2,7,11,15]target = 9return = [0,1]Indices 0 and 1 contain 2 and 7, whose sum is 9.
Example 2
nums = [1,4,2,3]target = 5return = [0,1]Both [0,1] and [2,3] work. The smaller first index selects [0,1].
Constraints
0 <= nums.length <= 1000.-1000000 <= nums[i] <= 1000000.-2000000 <= target <= 2000000.