-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC39.py
More file actions
29 lines (22 loc) · 762 Bytes
/
LC39.py
File metadata and controls
29 lines (22 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
if target == 0:
return []
ans = []
def backtrack(path,index,total,target):
if total > target:
return
if total == target:
ans.append([i for i in path])
return
for i in range(index,len(candidates)):
path.append(candidates[i])
backtrack(path,i,total+candidates[i],target)
path.pop()
backtrack([],0,0,target)
return ans