Skip to content

Commit

Permalink
Time: 68 ms (49.16%), Space: 16.6 MB (64.30%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
SDL101 committed Feb 11, 2024
1 parent dd955f3 commit 97682c4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 0040-combination-sum-ii/0040-combination-sum-ii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()

res = []

def backtrack(cur, pos, target):
if target == 0:
res.append(cur.copy())
return
if target <= 0:
return

prev = -1
for i in range(pos, len(candidates)):
if candidates[i] == prev:
continue
cur.append(candidates[i])
backtrack(cur, i + 1, target - candidates[i])
cur.pop()
prev = candidates[i]

backtrack([], 0, target)
return res

0 comments on commit 97682c4

Please sign in to comment.