Skip to content

Commit aa48b14

Browse files
authored
Create 40-Combination-Sum-II.py
1 parent 86159ad commit aa48b14

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

40-Combination-Sum-II.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
3+
candidates.sort()
4+
5+
res = []
6+
def backtrack(cur, pos, target):
7+
if target == 0:
8+
res.append(cur.copy())
9+
if target <= 0:
10+
return
11+
12+
prev = -1
13+
for i in range(pos, len(candidates)):
14+
if candidates[i] == prev:
15+
continue
16+
cur.append(candidates[i])
17+
backtrack(cur, i + 1, target - candidates[i])
18+
cur.pop()
19+
prev = candidates[i]
20+
21+
backtrack([], 0, target)
22+
return res

0 commit comments

Comments
 (0)