-
Notifications
You must be signed in to change notification settings - Fork 612
/
39.py
33 lines (25 loc) · 1019 Bytes
/
39.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
28
29
30
31
32
33
'''
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
'''
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
result = []
def recursive(candidates, target, currList, index):
if target < 0:
return
if target == 0:
result.append(currList)
return
for start in range(index, len(candidates)):
recursive(candidates, target - candidates[start], currList + [candidates[start]], start)
recursive(candidates, target, [], 0)
return result