fromtypingimportListdeftwoSum(nums:List[int],target:int)->List[int]:"""
Finds two numbers in a list that sum up to the target value.
Args:
nums (List[int]): A list of integers.
target (int): The target value.
Returns:
List[int]: A list containing the indices of the two numbers that sum up to the target.
Examples:
>>> nums = [2, 7, 11, 15]
>>> target = 9
>>> result = twoSum(nums, target)
>>> print(result)
[0, 1]
"""max_index=len(nums)-1start,end=0,1whilestart!=end:ifnums[start]+nums[end]==target:break# Setting up next iterationifend<max_index:end+=1elifstart<max_index:start+=1end=start+1returnstart,end
fromtypingimportListdeftwoSum(nums:List[int],target:int)->List[int]:"""
Finds two numbers in a list that sum up to the target value.
Args:
nums (List[int]): A list of integers.
target (int): The target value.
Returns:
List[int]: A list containing the indices of the two numbers that sum up to the target.
Examples:
>>> nums = [2, 7, 11, 15]
>>> target = 9
>>> result = twoSum(nums, target)
>>> print(result)
[0, 1]
"""summands={}fori,numberinenumerate(nums):ifnumberinsummands:breaksummands[target-number]=ireturnsummands[number],i