# leetcode —— 39. 組合總和

• 所有數字（包括 target）都是正整數。
• 解集不能包含重複的組合。

[
[7],
[2,2,3]
]

``````# Python3
class Solution:
def __init__(self):
self.ans = []
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
if not candidates:
return []
def combSums(candidates,target,temp,sums):
if target == sums:  # 遞歸停止條件
self.ans.append(temp[:])
return

for n in candidates:  # 遍歷每種情況
if sums + n > target:  # 如果當前sums值大於target，直接break
break
if temp and n < temp[-1]:  # 如果當前輸入的值小於temp[-1]則continue
continue
temp.append(n)
combSums(candidates,target,temp,sums+n)
temp.pop()  # 回溯
candidates.sort()  # 執行遞歸時先進行排序
combSums(candidates,target,[],0)
return self.ans
``````

``````# C++
class Solution {
private:
vector<vector<int>> num;
vector<int> sum;
int i = 0;
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
sort(candidates.begin(),candidates.end());
function(candidates,target,sum,i);
return num;
}
void function(vector<int>& candidate, int target,vector<int> sum,int i)
{
if(target==0)
{
num.push_back(sum);
}
for(;i<candidate.size();++i)  # 只遍歷比當前數字大的candidates數字
{
if(candidate[i]<=target)
{
sum.push_back(candidate[i]);
function(candidate,target-candidate[i],sum,i);
sum.pop_back();
}
}
}
};
``````