CodingEasy
Two Sum
You’re given an array of integers nums and an integer target. Return the indices of the two numbers in nums that add up to target.
Every input has exactly one answer, and the same element can’t be used twice. Return the two indices in either order.
Example 1
Input
nums = [2,7,11,15], target = 9Output[0,1]nums[0] + nums[1] = 2 + 7 = 9.
Example 2
Input
nums = [3,2,4], target = 6Output[1,2]The 3 can’t pair with itself, so the answer is 2 + 4.
Example 3
Input
nums = [3,3], target = 6Output[0,1]Constraints
- 2 ≤ nums.length ≤ 10⁴
- -10⁹ ≤ nums[i] ≤ 10⁹
- -10⁹ ≤ target ≤ 10⁹
- Exactly one answer exists