Two Sum
Given an array of integers nums and an integer target, return the 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. The numbers at indices 0 and 1 sum up to 9 (2+7=9).
[ [ 3, 2, 4 ], 6 ]
Explanation. The numbers at indices 1 and 2 sum up to 6 (2+4=6).
[ [ 3, 3 ], 6 ]
Explanation. The numbers at indices 0 and 1 sum up to 6 (3+3=6).
Follow-up: Can you come up with an algorithm that is less than O(n^2) time complexity?
1. The numbers in the array and the target can be any integer (positive, negative, or zero). 2. The array `nums` will always have at least two elements. 3. There will be exactly one solution for each input set provided.
- Views
- 4