Two Sum / Hash Table
Problem Given an array of integers and a target value, return the indices of the two numbers that add up to the target. Exactly one such pair is guaranteed to exist, and you may not use the same element twice.
Input / Output
- Input: an integer array
numsand an integertarget. - Output: a pair of indices
[i, j]withnums[i] + nums[j] == targetandi != j.
Constraints
- 2 ≤ n ≤ 10^4.
- Exactly one valid answer exists.
- The same element may not be reused.
Example
nums = [2,7,11,15], target = 9→[0,1]becausenums[0] + nums[1] == 9.nums = [3,2,4], target = 6→[1,2].
added …