From c434927d51573ad27e2945ca03a2f910cf5bc588 Mon Sep 17 00:00:00 2001 From: Nishith Gadhiya Date: Thu, 1 Oct 2020 12:01:52 +0530 Subject: [PATCH] added two_sum.py added another simple program --- two_sum.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 two_sum.py diff --git a/two_sum.py b/two_sum.py new file mode 100644 index 0000000..91db4a9 --- /dev/null +++ b/two_sum.py @@ -0,0 +1,17 @@ +# Given the list of numbers and a target, We have to find two numbers such that the addition of two numbers in the list should be equal to the target. +#Input: list of numbers, target +#Output: Index of two numbers +class Solution: + def twoSum(self, nums, target): + hash={} + for i in range(len(nums)): + com = target - nums[i] + if com not in hash: + hash[nums[i]]=i + else: + return [hash[com],i] + +s1 = Solution() +nums = [2,7,11,15] +target = 9 +print(s1.twoSum(nums , target )) \ No newline at end of file