Pair with target sum
Easyarrayhash map
Given an array nums and a number target, exactly one pair of elements adds up to target. Return the indexes of the two, smaller index first.
You may not use the same element twice.
Example 1
Input: nums = [2,7,11,15], target = 9 Output: [0,1]
2 + 7 = 9.
Example 2
Input: nums = [3,2,4], target = 6 Output: [1,2]
Example 3
Input: nums = [3,3], target = 6 Output: [0,1]
Constraints
- 2 ≤ nums.length ≤ 10⁴
- -10⁹ ≤ nums[i] ≤ 10⁹
- Exactly one answer exists
Submit checks your solution against 10 tests, including hidden ones.
/**
* @param {int[]} nums
* @param {int} target
* @return {int[]}
*/
function twoSum(nums, target) {
}