-
Notifications
You must be signed in to change notification settings - Fork 0
/
40.py
22 lines (21 loc) · 792 Bytes
/
40.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def combinationSum2(self, candidates, target):
candidates.sort()
def backtracking(candidates, target, start_index, path, ans):
if sum(path)==target:
ans.append(path.copy())
return
if sum(path)>target:
return
ele=-1
for i in range(start_index, len(candidates)):
if candidates[i]!=ele:
path.append(candidates[i])
backtracking(candidates, target, i+1, path, ans)
ele=path.pop()
path, ans=[],[]
backtracking(candidates, target, 0, path, ans)
return ans
candidates = [10,1,2,7,6,1,5]
target = 8
print(Solution().combinationSum2(candidates, target))