forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0039.py
27 lines (23 loc) · 774 Bytes
/
0039.py
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
class Solution:
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
result = list()
candidates.sort()
self._combinationSum(candidates, target, 0, list(), result)
return result
def _combinationSum(self, nums, target, index, path, res):
if target == 0:
res.append(path)
return
if path and target < path[-1]:
return
for i in range(index, len(nums)):
self._combinationSum(nums, target-nums[i], i, path+[nums[i]], res)
if __name__ == '__main__':
candidates = [2,3,5]
target = 8
print(Solution().combinationSum(candidates, target))