Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. class Solution(object):
  2.  
  3. def current(candidates, ctree):
  4. path = ctree[1]
  5. new = []
  6. count = 0
  7. for lchild in ctree[0]:
  8.  
  9. for sub in candidates[count:]:
  10. add = []
  11.  
  12. if (lchild[0]-sub) > 0:
  13. add += [lchild[0] - sub]
  14. add[1:] = lchild[1:]
  15. add += [sub]
  16. new += [add]
  17. if lchild[0]-sub == 0:
  18. path += [lchild[1:] + [sub] ]
  19.  
  20. else:
  21. pass
  22. count += 1
  23.  
  24. return [new, path]
  25.  
  26.  
  27. def combinationSum(self, candidates, target):
  28. if isinstance(target, int):
  29. tree = [[[target]],[]]
  30. else:
  31. tree = target
  32. start = [candidates, tree]
  33. test = current(candidates, tree)
  34. if test[0] == []:
  35.  
  36. return tree[1]
  37. else:
  38. combinationSum(candidates, test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement