Created
March 25, 2017 05:39
-
-
Save revanthpobala/8e1de890e09c12d9654471d16ef9b1f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Given a target, find the list of numbers that sum to the target | |
""" | |
def targetSum(a,k): | |
s={0:[]} | |
res = [] | |
for x in a: | |
ns=dict(s) | |
for j in s: | |
if j+x == k: | |
res.append(s[j]+[x]) | |
ns[j+x]=s[j]+[x] | |
s=ns | |
return res | |
print targetSum([-3,-5,9,12,0,3,2], 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment