Junior — Senior
Determining the indices of a pair of numbers with the required sum
livecode
Task condition
Given an array of integers nums and a target value target. Return the indices of two different elements such that their sum is equal to target.
It is guaranteed that there is exactly one correct solution, and the same element cannot be used twice. The order of the indices in the answer does not matter.
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
Examples:
- Input:
nums = [2,7,11,15],target = 9Output:[0,1] - Input:
nums = [3,2,4],target = 6Output:[1,2] - Input:
nums = [3,3],target = 6Output:[0,1]
Constraints:
2 <= len(nums) <= 10⁴-10⁹ <= nums[i] <= 10⁹-10⁹ <= target <= 10⁹- There is exactly one valid solution.