Skip to content

Instantly share code, notes, and snippets.

@viveksyngh
Created October 4, 2015 16:41
Show Gist options
  • Select an option

  • Save viveksyngh/cdc05d1cc18151d48e4f to your computer and use it in GitHub Desktop.

Select an option

Save viveksyngh/cdc05d1cc18151d48e4f to your computer and use it in GitHub Desktop.
Given an array of integers, find two numbers such that they add up to a specific target number.
__author__ = 'Vivek'
#Given an array of integers, find two numbers such that they add up to a specific target number.
class Solution:
# @param A : tuple of integers
# @param B : integer
# @return a list of integers
def twoSum(self, A, B):
Map = {}
for i in range(len(A)) :
if A[i] in Map :
return [Map[A[i]] + 1, i + 1]
else :
if B - A[i] not in Map :
Map[B - A[i]] = i
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment