```def combinationSum2(candidates, target):
res = []
candidates.sort()

def dfs(target, index, path):
if target < 0:
return  # backtracking
if target == 0:
res.append(path)
return  # backtracking
for i in range(index, len(candidates)):
if candidates[i] == candidates[i-1]:
continue
dfs(target-candidates[i], i+1, path+[candidates[i]])

dfs(target, 0, [])
return res```
```def combinationSum2(candidates, target):
res = []
candidates.sort()

def dfs(target, index, path):
if target < 0:
return  # backtracking
if target == 0:
res.append(path)
return
for i in range(index, len(candidates)):
dfs(target-candidates[i], i, path+[candidates[i]])

dfs(target, 0, [])
return res```
```def combine(self, n: int, k: int) -> List[List[int]]:
res = []
nums = range(1,n+1);

def dfs(k, index, path):
if k == 0:
res.append(path)
return # backtracking
for i in range(index, len(nums)):
dfs(k-1, i+1, path+[nums[i]])

dfs(k, 0 ,[])
return res```
star

Thu Mar 10 2022 02:32:24 GMT+0000 (Coordinated Universal Time) https://leetcode.com/problems/combination-sum/discuss/429538/General-Backtracking-questions-solutions-in-Python-for-reference-%3A

#python #template #combinations #sum
star

Thu Mar 10 2022 02:31:32 GMT+0000 (Coordinated Universal Time) https://leetcode.com/problems/combination-sum/discuss/429538/General-Backtracking-questions-solutions-in-Python-for-reference-%3A

#python #template #combinations #sum
star

Thu Mar 10 2022 02:19:49 GMT+0000 (Coordinated Universal Time) https://leetcode.com/problems/combination-sum/discuss/429538/General-Backtracking-questions-solutions-in-Python-for-reference-%3A

#python #combinations #template