Two Sum
Given an array of integers nums and an integer target, find the two different positions i and j whose numbers add up to target, and return them as [i, j] with i < j. Positions start at 0.
Every input has exactly one such pair.
Constraints
- 2 ≤ length of
nums≤ 10,000 - -100,000,000 ≤
nums[i]≤ 100,000,000 - -200,000,000 ≤
target≤ 200,000,000 - Exactly one pair adds up to
target.
Example 1
Input
nums = [2,7,11,15]
target = 9
Output
[0,1]
Why:
nums[0] + nums[1] is 2 + 7 = 9.
Example 2
Input
nums = [3,2,4]
target = 6
Output
[1,2]
Why:
2 + 4 = 6. Position 0 can't be used twice for 3 + 3.
Example 3
Input
nums = [3,3]
target = 6
Output
[0,1]
Why:
The same value can appear at two positions.
Hints
Was this a good challenge?
- Solved by
- 1
- Submissions
- 1
- Accepted
- 100%