LeetCode40. 组合总和 II
题目链接
https://leetcode.cn/problems/combination-sum-ii/
代码
python
class Solution:
def backtrackingz(self, candidates, target, result, total, path, startindex):
if target == total:
result.append(path[:])
return
for i in range(startindex, len(candidates)):
if i > startindex and candidates[i] == candidates[i-1]:
continue
if total + candidates[i] > target:
break
path.append(candidates[i])
total += candidates[i]
self.backtrackingz(candidates, target, result, total, path, i + 1)
total -= candidates[i]
path.pop()
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
result = []
candidates.sort()
self.backtrackingz(candidates, target, result, 0, [], 0)
return result