Combination Sum

Combination Sum

dfs version - 112ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = []
candidates.sort()
def dfs(target, index, path):
if target < 0:
return
if target == 0:
res.append(path)
return
for i in range(index, len(candidates)):
dfs(target - candidates[i], i, path + [candidates[i]])
dfs(target, 0, [])
return res

leet code