Two Sum
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.
[ [ 2, 7, 11, 15 ], 9 ]
Explanation. nums[0] + nums[1] = 2 + 7 = 9, so we return [0, 1].
[ [ 3, 2, 4 ], 6 ]
Explanation. nums[1] + nums[2] = 2 + 4 = 6, so we return [1, 2].
[ [ 3, 3 ], 6 ]
Explanation. nums[0] + nums[1] = 3 + 3 = 6, so we return [0, 1].
[ [ -1, -2, -3, -4, -5 ], -8 ]
Explanation. nums[2] + nums[4] = -3 + -5 = -8, so we return [2, 4].
Follow-up: Can you come up with an algorithm that is less than O(n^2) time complexity?
The length of `nums` will be at least 2 and less than 10^4. Each element in `nums` will be an integer between -10^9 and 10^9.
- Views
- 4