You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution:
defcombinationSum2(self, candidates: List[int], target: int) ->List[List[int]]:
res= []
combination= []
candidates.sort() # Sort `candidates` so we can skip duplicate numbersdefbacktrack(i, target):
# Base case - the target sum has been exceeded or all `candidates` have been usediftarget<=0ori>=len(candidates):
iftarget==0:
res.append(combination.copy())
returnprevious=-99# Actions - use each value in `candidates`forjinrange(i, len(candidates)):
# Skip duplicatesifcandidates[j] ==previous:
continuecombination.append(candidates[j])
# Backtrack using the next number & calculating the new targetbacktrack(j+1, target-candidates[j])
combination.pop()
previous=candidates[j]
backtrack(0, target)
returnres
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: