Simple backtracking


#1

void sum(int str,vector&A,int B, vector &v,vector<vector> &ans){

if(B==0){
ans.push_back(v);
    return ;
}
else if(B<0)
return;
if(str>A.size()-1)return ;
v.push_back(A[str]);
sum(str,A,B-A[str],v,ans);
v.pop_back();
if(B>=A[str+1])
sum(str+1,A,B,v,ans);

}
vector<vector > Solution::combinationSum(vector &A, int B) {
vector<vector> ans;
vector v;
sort(A.begin(), A.end());
vector::iterator ip;
std::sort(A.begin(), A.end());

ip = std::unique(A.begin(), A.end()); 

A.resize(std::distance(A.begin(), ip)); 
sum(0,A,B,v,ans);


return ans;

}