Skip to content

Instantly share code, notes, and snippets.

@coldmanck
Created May 15, 2020 11:13
Show Gist options
  • Save coldmanck/7c9e40cd60632b73b9338967d80c7973 to your computer and use it in GitHub Desktop.
Save coldmanck/7c9e40cd60632b73b9338967d80c7973 to your computer and use it in GitHub Desktop.
LeetCode 0063 combination sum (Backtrack)
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
def combination_sum(cur_ans, cur_sum, cand_idx):
if cur_sum >= target:
if cur_sum == target:
ans.append(cur_ans)
return
for i in range(cand_idx, len(candidates)):
combination_sum(cur_ans + [candidates[i]], cur_sum + candidates[i], i)
ans = []
combination_sum([], 0, 0)
return ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment