class Solution {
private:
vector<vector<int>> ans;
vector<int> count;
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
sort(num.begin(),num.end());
count.resize(num.size());
ans.clear();
solve(target,num,0,num.size());
return ans;
}
void solve(int target,vector<int> &num,int index,int maxDep)
{
if(target<0)
return;
if(target==0)
{
vector<int> temp;
temp.clear();
for(int i = 0;i<index;i++)
for(int j = 0;j<count[i];j++)
{
temp.push_back(num[i]);
}
ans.push_back(temp);
return;
}
if(index==maxDep)
return ;
for(int i =0;i<=min(target/num[index],1);i++)
{
count[index] = i;
if(index>0&&i==1&&num[index]==num[index-1]&&count[index-1]==0)//判断第二个1是否能取。能去的条件就是第一个1取了。
continue;
solve(target-i*num[index],num,index+1,maxDep);
}
}
};
Combination SumII
最新推荐文章于 2024-11-01 15:19:21 发布