Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions combination sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
self.result=[]
self.helper(candidates,target,0,[])
return self.result
def helper(self,candidates,target,i,path):

#basecase
if target<0 or i==len(candidates):
return
if target==0:
self.result.append(path)
return

#nochoose case
self.helper(candidates,target,i+1,list(path))
#choose case
path.append(candidates[i])
self.helper(candidates,target-candidates[i],i,list(path))