1. Pair to Target
easyarrayhash map
Given an integer array nums and an integer target, return the indices [i, j] (with i < j) of the two numbers that add up to target.
Exactly one valid pair exists, and you may not use the same element twice.
Example 1
Inputnums = [2,7,11,15], target = 9
Output[0,1]
Example 2
Inputnums = [3,2,4], target = 6
Output[1,2]
Example 3
Inputnums = [3,3], target = 6
Output[0,1]
Constraints
- 2 ≤ nums.length ≤ 10⁴
- -10⁹ ≤ nums[i], target ≤ 10⁹
- Exactly one answer exists
Hint 1
For each number, the partner you need is `target - x`.
Hint 2
A hash map from value to index gives O(n).