programming-examples/python/Class/Python program to find a pair of elements (indices of the two numbers) from a given array whose sum equals a specific target number.py

8 lines
338 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
class py_solution:
def twoSum(self, nums, target):
lookup = {}
for i, num in enumerate(nums):
if target - num in lookup:
return (lookup[target - num] + 1, i + 1)
lookup[num] = i
print("index1=%d, index2=%d" % py_solution().twoSum((10,20,10,40,50,60,70),50))