-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path82_combinationSum.py
More file actions
29 lines (29 loc) · 1.11 KB
/
82_combinationSum.py
File metadata and controls
29 lines (29 loc) · 1.11 KB
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
'''39. Combination Sum
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
Example 1:
Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.'''
from collections import List
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
def backtracking(candidates,target,total,ans,tempArr,idx):
if idx==len(candidates):return
if total==target:
ans.append(tempArr[:])
return
if total>target:
return
total+=candidates[idx]
tempArr.append(candidates[idx])
backtracking(candidates,target,total,ans,tempArr,idx)
item=tempArr.pop()
total-=item
backtracking(candidates,target,total,ans,tempArr,idx+1)
ans=[]
tempArr=[]
backtracking(candidates,target,0,ans,tempArr,0)
return ans