Description
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
Examples
Input:
nums = [2,7,11,15], target = 9Output:
[0,1]Explanation:
nums[0] + nums[1] = 2 + 7 = 9, so the answer is [0, 1].
Input:
nums = [3,2,4], target = 6Output:
[1,2]Explanation:
nums[1] + nums[2] = 2 + 4 = 6, so the answer is [1, 2].
Input:
nums = [3,3], target = 6Output:
[0,1]Explanation:
Same values at different indices: nums[0] + nums[1] = 3 + 3 = 6.
Input:
nums = [-1,-2,-3,-4,-5], target = -8Output:
[2,4]Explanation:
Works with negative numbers: nums[2] + nums[4] = -3 + -5 = -8.
Constraints
- •
2 ≤ nums.length ≤ 10⁴ - •
-10⁹ ≤ nums[i] ≤ 10⁹ - •
-10⁹ ≤ target ≤ 10⁹ - •
Only one valid answer exists.