Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
- def backtrack(remain: int, combs: List[int], index: int) -> None:
- if remain == 0:
- # this ensures that each solution stored in results
- # is independent of future modifications to combs.
- results.append(list(combs))
- if remain < 0:
- return
- # recursive: append the number
- for i in range(index, len(candidates)):
- combs.append(candidates[i])
- backtrack(remain - candidates[i], combs, i)
- combs.pop()
- results = []
- backtrack(target, [], 0)
- return results
Advertisement
Add Comment
Please, Sign In to add comment