题目
struct Node
{
vector a;
int sum;
int index;
Node(){}
Node(int index,int sum,vector a)
{
this->index = index;
this->sum = sum;
this->a = a;
}
};
class Solution {
public:
queue q;
map m;
vector> combinationSum2(vector& candidates, int target) {
sort(candidates.begin(),candidates.end());
vector> ans;
for(int i=0;i a;
a.push_back(candidates[i]);
q.push(Node(i,candidates[i],a));
m[candidates[i]]=1;
}
}
while(!q.empty())
{
Node term = q.front();
q.pop();
if(term.sum==target)
{
ans.push_back(term.a);
continue;
}
if(term.sum>target)
{
continue;
}
m.clear();
for(int i=term.index+1;itarget)
continue;
else if(term.sum+candidates[i]==target)
{
vector x = term.a;
x.push_back(candidates[i]);
ans.push_back(x);
m[candidates[i]]=1;
}
else
{
vector x = term.a;
x.push_back(candidates[i]);
q.push(Node(i,term.sum+candidates[i],x));
m[candidates[i]]=1;
}
}
}
return ans;
}
};