Best C++ recursive solution


#1

vector<vector> v;

void PermuteHelper(vector s, vector aux)
{
if (s.size() == 0) v.push_back(aux);
else
{
for (int i = 0 ; i < s.size(); i++)
{
int c = s[i]; // choose
aux.push_back©;
s.erase(s.begin()+i);
PermuteHelper(s,aux); // explore
s.insert(s.begin()+i,c); //un-choose
aux.pop_back();
}
}
}

vector<vector > Solution::permute(vector &A) {
vector a;
v.clear();
PermuteHelper(A,a);
return v;
}